This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The "new" creates an object. There are two different String literals, each of which is an object. And the catenation produces a new object. That's four. I don't know what you mean by "lost" Strings; it's not a technical term with any common meaning.
In any event, this kind of nit-picky question is off-topic here; this sort of argument belongs in SCJP, where I will move it.
------------------------------------------------------------ Ans: c. Line 13 creates two, one referred to by x and the lost string "xyz". Line 14 creates one. Line 15 creates one. --------------------------------------------------------------
Actually, line 13 creates two objects. One in heap and one in literal pool. That's why you will get 2 objects at line 13.
SCJP1.4<br />SCWCD (in progress)
Joined: Jun 20, 2005
Thanks for your reply.
I am somewhat closer to satisfaction.
does it mean that the new statement creates two objects for every non-intern (unique string not in string pool) string like in line 13? Please do answer for confirmation.
Originally posted by Marzo Kaahn: can someone plz clarify this.
The literal is itself a String object. It is indeed referenced in the literal pool, although that's neither here nor there. But the "new String()" creates a copy of that literal, and it's that copy that's assigned to the variable. Those are the two Strings.
Note that it's bogus (although common!) to say that the literal object is created at this line. It's not, really; it's created when the class is loaded -- unless the String pool already contains a literal with this value, in which case the pre-existing one is used instead. Nevertheless, the exam uses this wording, so you need to be able to think of it this way.
Note that 99.999% of the time, using this String constructor is unnecessary and simply wrong. Strings are immutable objects, and so it's simply never necessary to copy one. Whenever you see this in real code, you know you're looking at code written by a newbie. By far, the most common usage of this constructor is as part of the SCJP exam!
�3.10.5 String Literals: Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern.
What is the String Literal Pool? Most often, I hear people say that it is a collection of String objects. Although that's close, it's not exactly correct. Really, it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.
The above quote by Steve Morrow. It is wrong. Please refer "Creating New Strings" section of Chapter 6 (under important facts about Strings and memory) of K&B book.
It's perfectly correct to say the literal pool is a "pool of String objects." But when reading that, you must understand that it means the references are stored in this pool, while the objects themselves are stored in the Java heap. Don't let all this language-lawyer stuff get in the way of your common sense!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com