aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String new object clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String new object clarification" Watch "String new object clarification" New topic
Author

String new object clarification

Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
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

Joined: Oct 17, 2008
Posts: 171

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

Joined: Feb 10, 2007
Posts: 63
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

Joined: Aug 29, 2008
Posts: 580

Is there any way to find what are the literals in the String pool ?


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String new object clarification