File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects ?

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
How many objects are created in the following statement ?
String s = new String("Hello");
 
paul wheaton
Trailboss
Pie
Posts: 21187
Firefox Browser IntelliJ IDE Java
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One whose reference (address?) is stored in variable (not Object), <CODE>s</CODE>
 
paul wheaton
Trailboss
Pie
Posts: 21187
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to say Tony?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21187
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic