• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Numer of Strings:-2
Number of character strings:-1
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic