This question is from -edit- an illegal source -/edit-
Ruben, I didn't get the point. Can you explain a bit clear...
The String objects corresponding to the string literals are created before the method is executed, that's why you are only creating 3 String objects in the method.
Joined: Dec 16, 2008
When a class is loaded, java analyzes the source file and when it sees a string literal, it creates a String object for it, and adds a reference to it in the String Constant Pool (assuming a string literal with the same contents has not already been added.) That's why the String objects for string literals exist even before the main method starts executing. Take a look at this (The Java Language Specification):
A new class instance may be implicitly created in the following situations:
* Execution of a string concatenation operator (§15.18.1) that is not part of a constant expression sometimes creates a new String object to represent the result. String concatenation operators may also create temporary wrapper objects for a value of a primitive type.
Here concatenation opearator means only operator(+) or concat method also ???
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.