Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many string objects ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many string objects ?" Watch "How many string objects ?" New topic
Author

How many string objects ?

lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
Hi,
I have doubt regarding answer to the following question in K&B :


How many String objects have been created ?
a. 2
b. 3
c. 4
d. 5

Ans: c. Line 13 creates two, one referred to by x and the lost string "xyz". Line 14 creates one. Line 15 creates one.

Afer banging my head again and again on the question i could not convince myself for the answer. PLEASE HELP? Why is it traeting the lost string object as another object for reconsideration. ?


SCJP1.4, SCWCD1.4, SCBCD5.0(working on...)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Kannan Raju
Ranch Hand

Joined: Jul 07, 2005
Posts: 44
------------------------------------------------------------
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)
lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
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.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

One in heap and one in literal pool.
No; the strings are created on the heap. The literal pool stores references, not objects.
[ July 11, 2005: Message edited by: Steve Morrow ]
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

can someone plz clarify this.

String str1 = new String("abc");

having the above (and judging from the replies), would the following be true:

1) a String REFERENCE is created in the literal pool which refers to a...
2) a String OBJECT is created on the heap.

if true, then only one object is being created.
if false, how is there two String OBJECTS instead of one?



SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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!
Kannan Raju
Ranch Hand

Joined: Jul 07, 2005
Posts: 44
--------------------------------------------------------------------------------

No; the strings are created on the heap. The literal pool stores references, not objects.
------------------------------------------------------------------------

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.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Please refer "Creating New Strings" section of Chapter 6 (under important facts about Strings and memory) of K&B book.
For those of us without the benefit of having that book in our hands, please provide the quotation that contradicts my statement.

In support of the premise that the literal pool is a collection of references to String objects on the heap, I present the following from the Java� Language Specification:

�4.3.3 The Class String:
String literals (�3.10.5) are references to instances of class String.

�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.

Other documentation here at JavaRanch supports the specification as well:
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.


I'd be interested to see what the K&B book says.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

The Virtual Machine specification also describes the runtime constant pool as as a table of references, not objects.

http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#67960
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Kannan Raju:

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: How many string objects ?
 
Similar Threads
Object creation
String Objects
Definition of a Java object
String Objects Confusion
String Object query