mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-05 06:17:36 +02:00
Add StringBuilder usage tip
This commit is contained in:
@@ -190,7 +190,20 @@ public class LearnJava {
|
|||||||
builderConcatenated.append("the StringBuilder class.");
|
builderConcatenated.append("the StringBuilder class.");
|
||||||
System.out.println(builderConcatenated.toString()); // only now is the string built
|
System.out.println(builderConcatenated.toString()); // only now is the string built
|
||||||
// Output: You can use the StringBuilder class.
|
// Output: You can use the StringBuilder class.
|
||||||
|
|
||||||
|
// StringBuilder is efficient when the fully constructed String is not required until the end of some processing.
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
String inefficientString = "";
|
||||||
|
for(int i = 0 ; i < 10; i++){
|
||||||
|
stringBuilder.append(i).append(" ");
|
||||||
|
inefficientString += i + " ";
|
||||||
|
}
|
||||||
|
System.out.println(inefficientString);
|
||||||
|
System.out.println(stringBuilder.toString());
|
||||||
|
// inefficientString requires a lot more work to produce, as it generates a String on every loop iteration.
|
||||||
|
// Simple concatenation with + is compiled to a StringBuilder and toString()
|
||||||
|
// Avoid string concatenation in loops.
|
||||||
|
|
||||||
// #3 - with String formatter
|
// #3 - with String formatter
|
||||||
// Another alternative way to create strings. Fast and readable.
|
// Another alternative way to create strings. Fast and readable.
|
||||||
String.format("%s may prefer %s.", "Or you", "String.format()");
|
String.format("%s may prefer %s.", "Or you", "String.format()");
|
||||||
|
Reference in New Issue
Block a user