From bb504b97ca06322cc131105647943f0140d6e313 Mon Sep 17 00:00:00 2001 From: Andrew Gallasch Date: Thu, 27 Oct 2016 09:50:16 +1030 Subject: [PATCH] Add StringBuilder usage tip --- java.html.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/java.html.markdown b/java.html.markdown index 6487d862..6753b358 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -190,7 +190,20 @@ public class LearnJava { builderConcatenated.append("the StringBuilder class."); System.out.println(builderConcatenated.toString()); // only now is the string built // 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 // Another alternative way to create strings. Fast and readable. String.format("%s may prefer %s.", "Or you", "String.format()");