wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects ?" Watch "How many objects ?" New topic
Author

How many objects ?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
How many objects are created in the following statement ?
String s = new String("Hello");
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20495
    ∞

one.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
One whose reference (address?) is stored in variable (not Object), <CODE>s</CODE>


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20495
    ∞

What are you trying to say Tony?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
This statement will cause the creation of two objects - on for the string literal, and one for the string created by the String(String) constructor. However the first object will be created at class loading time, rather than later when the line gets executed.
Note - if any other lines in this or any other class also contain the string "Hello", then there is still only one "Hello" created at class load time (for whichever class loads first that has the string in it). So then you can't really say whether this line in particular "created" it - it would have been created anyway, and who knows which line is "responsible".
I don't believe real test questions are ever known to ask about how String literals work, so I wouldn't worry about it for certification.


"I'm not back." - Bill Harding, Twister
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Paul, I was trying to say that 's' should not be confused with the object itself, as I see newcomers do. That is, confuse the 's' in <CODE>String s = new String("ABC")</CODE> with the string object "ABC" itself. No more than that
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Paul, what I was thinking of was of a recent post in c.l.j.p about whether String objects were immutable or not. The person gave the example:
<PRE>
String s = new String("ABC");
System.out.println(s);
s = s + "DEF";
System.out.println(s);
</PRE>
as proof that strings were not immutable.



So you can imagine that the next 200 posts (OK, I'm exaggerating) were about how String object references are not to be confused with the String objects that these references reference. (Can you say that 3 times fast!?)

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20495
    ∞

I'm with you Tony!
Just to eliminate confusion... The question was How many objects are created in the following statement ?
String s = new String("Hello");
The answer is: one!
Sure, a string literal was made some time before you this line. And, s is a String reference to the brand new String object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects ?
 
Similar Threads
String objects
how many objects will create
String
String Object Creation
String and String Object Problem