• 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

String new object clarification

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

String s = new String ("abc");

1) Does the above single standalone statement create 2 objects?

2) How many total objects are created for the below two consecutive statements in a block? Is it 2 or 3?
--
String s1 ="abc";
String s1 = new String("abc");
------
As per K&B(pg. 434 SCJP6), s1=new String ("abc") will create 2 objects. One in nonpool memory and s1 will refere to it, and the 2nd object is created in the "String constant pool" memory area. I'm not sure whether 'new' always creates 2 objects or it creates 2 new objects only if the 'new String()' is preceded by another regular String assignment( in this case String s1 ="abc") having the same string value.
3) Is there any way to know of objects that are created but not assigned to any reference variable?
Thanks in advance.
Ajit
[ December 25, 2008: Message edited by: Ajit Sawant ]
 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) yes,if there is not already an "abc" in the pool.
2) First statment creates an object in the pool if there is not "abc" in the pool already.After first statmnt gets executed,2nd one will create just 1 object in the non pool memory.
3) ??? I don't think there is any way.

I hope this helped you partially atleast.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per K&B(pg. 434 SCJP6), s1=new String ("abc") will create 2 objects. One in nonpool memory and s1 will refere to it, and the 2nd object is created in the "String constant pool" memory area. I'm not sure whether 'new' always creates 2 objects or it creates 2 new objects only if the 'new String()' is preceded by another regular String assignment( in this case String s1 ="abc") having the same string value.

I think we need to simply remember this

when somebody says one object is created for sure. If "xyz" is not there in the string pool then it is created, irrespective of whether you do or don't have preceding it.

In your 3rd question i think there should be no way to know that logically. If there is somebody pointing to an object that is not set to a reference variable then that object is still having one reference, is it not? So its still having a reference and it would never be garbage collected. So i think there would be no way to find that unless some API gives that feature.

After seeing reflection API accessing private variables its very difficult say any such thing for sure though!
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to find what are the literals in the String pool ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic