Languages
[Edit]
EN

Java - optimal capacity value in StringBuilder

6 points
Created by:
Palusbug
515

In this short article, we would like to answer the question: what is the optimal value for StringBuilder capacity in the constructor?

Naive answer: the same like the amount of the characters will be stored inside.

e.g.

 

MySQL JDBC driver example

Edit

In MySQL JDBC driver, escaped value characters are added to the builder with capacity equals to:

The reason why 1.1 coefficient is used, is the typical amount of the characters after escaping, keeping in the mind, we don't want to reserve too much memory if it is not necessary - it is like empirical value.

JDBC source code part (com.mysql.cj.ClientPreparedQueryBindings class):

Where: x is not escaped yet value.

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join