diff options
Diffstat (limited to 'java.html.markdown')
-rw-r--r-- | java.html.markdown | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/java.html.markdown b/java.html.markdown index 13c75ffe..12de0c73 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -198,7 +198,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()"); @@ -835,6 +848,10 @@ The links provided here below are just to get an understanding of the topic, fee * [Java Code Conventions](https://www.oracle.com/technetwork/java/codeconvtoc-136057.html) +* New features in Java 8: + * [Lambda expressions (functional programming)](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html) + * [Date and time API (java.time package)](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) + **Online Practice and Tutorials** * [Learneroo.com - Learn Java](http://www.learneroo.com) |