diff options
| author | ven <vendethiel@hotmail.fr> | 2016-06-28 18:06:06 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-06-28 18:06:06 +0200 | 
| commit | 7812b99ff8f649595a57ef86577c7567cd1c8166 (patch) | |
| tree | da84c83c5dd2729bddff4dd0a3cea2b585c80674 /java.html.markdown | |
| parent | d3a31e9a7d2752e67e452a28f318710448444ba8 (diff) | |
Manually merge #1780
fixes #1780
Diffstat (limited to 'java.html.markdown')
| -rw-r--r-- | java.html.markdown | 24 | 
1 files changed, 24 insertions, 0 deletions
| diff --git a/java.html.markdown b/java.html.markdown index fb0913f1..f65c484e 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -8,6 +8,7 @@ contributors:      - ["Zachary Ferguson", "http://github.com/zfergus2"]      - ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]      - ["Rachel Stiyer", "https://github.com/rstiyer"] +    - ["Michael Dähnert", "http://github.com/JaXt0r"]  filename: LearnJava.java  --- @@ -171,6 +172,29 @@ public class LearnJava {          System.out.println(barString);          System.out.println(bazString); +        // String Building +        // #1 - with plus operator +        // That's the basic way to do it (optimized under the hood) +        String plusConcatenated = "Strings can " + "be concatenated " + "via + operator."; +        System.out.println(plusConcatenated); +        // Output: Strings can be concatenated via + operator. + +        // #2 - with StringBuilder +        // This way doesn't create any intermediate strings. It just stores the string pieces, and ties them together +        // when toString() is called. +        // Hint: This class is not thread safe. A thread-safe alternative (with some impact on performance) is StringBuffer. +        StringBuilder builderConcatenated = new StringBuilder(); +        builderConcatenated.append("You "); +        builderConcatenated.append("can use "); +        builderConcatenated.append("the StringBuilder class."); +        System.out.println(builderConcatenated.toString()); // only now is the string built  +        // Output: You can use the StringBuilder class. + +        // #3 - with String formatter +        // Another alternative way to create strings. Fast and readable. +        String.format("%s may prefer %s.", "Or you", "String.format()"); +        // Output: Or you may prefer String.format(). +           // Arrays          // The array size must be decided upon instantiation          // The following formats work for declaring an array | 
