aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects" Watch "String Objects" New topic
Author

String Objects

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
How many String objects are created when we run the following code.
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
Answer should be 2 String objects are created
The reference s2 and s1 both are pointing to "Hello"
and s3 and s4 both are pointing to "HelloPal"
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Indeed, the correct answer is 2. In general with questions like these, you can simply count the number of time the keyword "new" is used. However, as String literals are being used, you have to count the number of unique String literals. Remember String literals are shared so two instances of the same String literal will reference the same String object.
Corey
[ June 03, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Indeed, I put the same answer. But the mock exam in http://valiveru.tripod.com/java/jvaltest.html said it is 3. Also, I felt a lot of inconsistancies in that exam.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Actually, I was wrong! The correct answer is 3. After your last statement, I quickly saw what I missed. One of the String objects that is created isn't readily visible. Let's draw pictures to see what happens. As we're using String literals, I'm going to include the references created within the String constants table.

At this point, we have two references to this object. One by the variable s1 and one from the String constant table, I called it const1.

Now, we have three references to the same object.

Here is where I made my mistake. As the String literal "Pal" is used, a new object is created for that String and a reference is made to it from the String constant table. Then, this String in concatenated onto the existing object, which creates another new String object, which is referenced by s3.

Again, we add a reference to an existing object.
So, hopefully, you can see that there are, indeed, 3 String objects created. I guess my idea of counting String literals is shot.
If you have any more questions, please let me know.
Corey
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
Oh yes!! I also missed "Pal".
[ June 03, 2002: Message edited by: swati gupta ]
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
So Corey, you mean to say that the three String objects created are
1) Hello (referenced by s1,s2)
2) Pal (No Refernece)
3) Hello Pal (referenced by s3,s4)
Is that right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Thiru Thangavelu:
So Corey, you mean to say that the three String objects created are
1) Hello (referenced by s1,s2)
2) Pal (No Refernece)
3) Hello Pal (referenced by s3,s4)
Is that right?

Yes, that's right that there are three String objects, but don't forget about the constant references.
In Java, there is a table for String constants (much like primitive constants). This table contains a reference to each and every String literal. This is where the notion of the String literal pool comes from. So, the objects "Hello" and "Pal" have one extra reference, each, from the String constants pool. So, if you update your summary to this:
1) Hello (referenced by s1,s2, constant table)
2) Pal (referenced by constant table)
3) Hello Pal (referenced by s3,s4)
I'd say that this is more accurate.
Check out this thread for further discussion on the topic.
I hope that helps,
Corey
[ June 03, 2002: Message edited by: Corey McGlone ]
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Thanks Corey, that helps a lot to understand the constant tanle.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Objects