aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String" Watch "String" New topic
Author

String

Susie Chow
Ranch Hand

Joined: Jun 12, 2001
Posts: 33
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

Joined: Dec 16, 2000
Posts: 625
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

Joined: Jul 09, 2001
Posts: 2
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

Joined: Dec 16, 2000
Posts: 625
Yeah,
There are only two Strings created. Where are you getting this from that said there are 3?
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
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


green horn
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
I don't see how that makes it 3 Strings?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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!


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4692
    
    7

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).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Sometimes the answer is just staring you right in the face! Good catch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String