Banu Chowdary wrote:
public String makeString(){
String s="Fred"; // 1
s=s+"47"; // 2
s=s.substring(2,5); // 3
s=s.toUpperCase(); // 4
return s.toString(); // 5
}
The three objects created in the method are: "Fred47", "ed4", and "ED4".
Line by line:
1. No String object created at runtime (since "Fred" is a string literal.)
2. Here, we are creating "Fred47" (Notice that if the operation had been "Fred" + "47" no string would have been created at runtime, because you would be using the concatenation operator with two compile time constants (two string literals.))
3. A new String with the contents "ed4" is returned from the substring() method.
4. A new String with the contents "ED4" is returned from the toUpperCase() method.
5. No new String is created, since toString() returns the this reference.
As for the concatenation operator, that refers only to + and +=, yes.