• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

String

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many String objects are created when we run the following code.
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
The answers prints 3. Why is that? Thank you.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only see two Strings being created, one in line 1, and another in line 3. line 2 and line 4 reference existing strings.
Correct me if I'm wrong.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with:
----------------I only see two Strings being created, one in line 1, and another in line 3. line 2 and line 4 reference existing strings.------------------------------------
if you try:
public class Test{
public static void main(String[] args)
{
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
if(s2 == s1)
System.out.println("s1 == s2");
if(s3 == s4)
System.out.println("s3 == s4");
}
}
the result would be:
s2 == s1
s3 == s4
which means only two objects were created.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah,
There are only two Strings created. Where are you getting this from that said there are 3?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's 3. See JLS the end of 3.10.5:
quote:
1. Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
2. Literal strings within different classes in the same package represent references to the same String object.
3. Literal strings within different classes in different packages likewise represent references to the same String object.
4. Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
5. Strings computed at run time are newly created and therefore distinct.
6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
Hope it helps
Jason
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how that makes it 3 Strings?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only Strings created are s1 and s3. s2 and s4 are merely pointers to the existing Strings.
This is no different than this:
Button s1 = new Button("OK");
Button s2 = s1;
Button s3 = new Button("Cancel");
Button s4 = s3;
So how many Buttons were created? 2!
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you see questions like this in the exam, you need to take into account the String objects created dynamically and those that are created in the string pool. The following String objects are created by the given code:
<pre>
#1 - "Hello" (string pool)
#2 - "Pal" (string pool)
#3 - "HelloPal" (by 's2 + "Pal"')
</pre>
Therefore, three objects are created. s1 & s2 reference "Hello" while s3 & s4 reference "HelloPal". There are no references to the "Pal" String object.

[This message has been edited by JUNILU LACAR (edited July 09, 2001).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes the answer is just staring you right in the face! Good catch!
 
reply
    Bookmark Topic Watch Topic
  • New Topic