View on GitHub

Bearded-android-docs

StringBuilder

Download this project as a .zip file Download this project as a tar.gz file

Created Thursday 12 September 2013

If you use String concatenation in a loop (something like:

String s = "";
for(int i = 0; i < 100; i++) {
  s += ", " + i;
}

then you should use a StringBuilder (not StringBuffer) instead of a String, because it is much faster and consumes less memory.

If you have a single statement:

String s = "1, " + "2, " + "3, " + "4, " ....;
then you can use "Strings", because the compiler will use StringBuilder automatically.

Note: But it will use the default constructor, which means in the majority of cases, it will have to do a reallocation. However, go for readability unless you really need performance.

Links


No backlinks to this page.
comments powered by Disqus