• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Objects

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes!! I also missed "Pal".
[ June 03, 2002: Message edited by: swati gupta ]
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey, that helps a lot to understand the constant tanle.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic