• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String new object clarification

 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
paddy Mahadeva Iyer
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to find what are the literals in the String pool ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic