• 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

Count the number of object

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = new String("Hi");
String s1 = new String("Hello");
String s2 = "Hi";

The total number of object are 4 . Right ?
2 object on string literal pool .
2 object on garbage collection heap .

s is a reference variable that is pointing to an object that is on GCH & that object is poining to an object of SLP that has string literal Hi

s1 is a reference variable that is pointing to an object that is on GCH & that object is poining to an object of SLP that has string literal Hello

s2 is a reference that is directly pointing to the same object of SLP that has string literal Hi

Am I Right in everything ???

thanks .
[ January 08, 2005: Message edited by: rathi ji ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s and s2 are not the same object, despite containing the same content, so that makes 5. If the line initialising s2 read
s2 = s;
then there would be 4.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that Rathi is correct, but I don't know how to test his answer without looking at the bytecode.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is final answer ???
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,
Your explanation is absolutely correct. 4 objects.
[ January 08, 2005: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 1907
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I run javap command,I see the term 'new' only twice.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arjun Shastry:
If I run javap command,I see the term 'new' only twice.




Do you expect more? String literal objects are not created with new.
reply
    Bookmark Topic Watch Topic
  • New Topic