Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java 8 '+' operator for concatinating replaced by new StringBuilder() [duplicate]

In Java 8, I wrote some sample code.

String s1 = "Hello";  
String s2 = "world";  
String s3 = s1 + s2;  

After decompiling .class file I found that 3rd statement

 String s3 = s1 + s2;  

replaced by

 String s3 = new StringBuilder(s1).append(s2).toString();

Does it mean that There is no longer need to use explicit StringBuilder for Optimization and just use '+' operator instead of?

like image 767
shubham12511 Avatar asked Mar 13 '23 00:03

shubham12511


1 Answers

Yes. Actually, this optimization had been done in Java 6. See Bruce Eckel's "Thinking in Java" 4th edition pp.356-359 for details

like image 130
Vladimir Vagaytsev Avatar answered Mar 15 '23 05:03

Vladimir Vagaytsev