Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Susie Chow
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • 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.
 
kelly, zhu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yeah,
There are only two Strings created. Where are you getting this from that said there are 3?
 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I don't see how that makes it 3 Strings?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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!
 
Junilu Lacar
Bartender
Pie
Posts: 7327
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes the answer is just staring you right in the face! Good catch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic