But I think total 5 objects will be created...: 1)Fred 2) 47 3)Fred47 4)ed4 5)ED4
Am I right ??? or If the answer given is right .. how it will be???
Kumaresh Vidhyasagar
Ranch Hand
Joined: Dec 05, 2008
Posts: 30
posted
0
Your answer is correct!
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
Banu,
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.
All code in my posts, unless a source is explicitly mentioned, is my own.
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
posted
0
Hi Banu,
It's important to always include the source of any mock question that you discuss on this forum.
Where did this question come from?
Thanks,
Bert
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Balaji Bang
Ranch Hand
Joined: Apr 23, 2007
Posts: 180
posted
0
Hi
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.
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
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):
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.5
Balaji Bang
Ranch Hand
Joined: Apr 23, 2007
Posts: 180
posted
0
so in this method those 3 objects are : 1) Fred 2) 47 3)Fred47 . Correct?
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.
This exact same question has been discussed many times before on JavaRanch. Please do a search for "Fred 47" in the forums, and you'll find a whole list of old discussions: