• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How many objects ?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are created in the following statement ?
String s = new String("Hello");
 
Trailboss
Posts: 23548
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One whose reference (address?) is stored in variable (not Object), <CODE>s</CODE>
 
paul wheaton
Trailboss
Posts: 23548
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you trying to say Tony?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23548
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's never done THAT before. Explain it to me tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic