Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question

 
Andrei Pop
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the following piece of code:

String s1 = "abc";
String s2 = s1;
String s3 = "abc";

How many objects are created? My answer is: 2.

What do you think?

Regards,
Andrei
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numer of Strings:-2
Number of character strings:-1
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrei,
I think there should be only 1 string literal object in the String constant pool.
[ November 09, 2004: Message edited by: Jay Pawar ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrei,

I agree with Jay.

You can verify that by using == operator to determine if s1 and s3 reference to the same object.



Joyce
[ November 10, 2004: Message edited by: Joyce Lee ]
 
Hai Le
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as for me i think there is one object and 3 reference. Correct my thought if i'm wrong...!!
 
Andrei Pop
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answers.

The authors of the mock exam think there is one object only, the one in the constant string pool. But there are 2 string objects created.

Andrei
 
Nandish Sri
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As I think there are 2 String Objects and 3 references are created,
1 String Object in String pool and One String Object in HEAP.
Correct me If I'm wrong !

Regards,
Nandish
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe object will be placed on heap only if it is created by using new keyword. I agree with thixtymilk... 1 string object and 3 references.


Do you still think there will be 100 objects ?
Since Strings are immutable, it is good way to save memory by just creating 100 references refering (pointing) to only 1 String literal object in String constant pool.
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is from JLS

"Literal strings within different classes in different packages likewise represent references to the same String object. " - doesnt this mean that there is only one string object ?
 
Andrei Pop
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay, you are right. I knew about the string pool, but I sticked to the immutable behaviour of the String class. In the K&B book there's not extensive coverage for the case we discussed.

Thanks,
Andrei
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrei,
I am reading K&B book and I love the writing style of both authors and the way they have managed to present the facts. As a matter of fact, I gave my answer based on what I read from the same book. Here is the extract



To make Java more memory efficient,the JVM sets aside a special area of memory called the �String constant pool.� When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.


Page 359.


Hope this helps you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic