String/ object creation/ efficiency
Suppose you have some code that looks like this
Code:
StringBuilder str = new StringBuilder();
str.append("one");
str.append("two");
System.out.println(str.toString() + "three");
System.out.println(str.toString() + "four");
System.out.println(str.toString() + "five");
Question 1: In this example, str.toString() conversions happens 3 times separately, or Java knows to reuse the results of conversion done once locally?
Question 2. Would any efficiencies be introduced by changing the code to
Code:
StringBuilder str = new StringBuilder();
str.append("one");
str.append("two");
String x = str.toString();
System.out.println(x + "three");
System.out.println(x + "four");
System.out.println(x + "five");
Please let me know