This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String literal - strange problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String literal - strange problem" Watch "String literal - strange problem" New topic
Author

String literal - strange problem

Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
public class Test {
public static void main (String [] argv)
{
String s1="ab";
String s2="abcd";
String s3="cd";
String s4=s1+s3;
System.out.println(s2 == s4);
}
}
Output: False
RHE describes clearly that NO new string literal will be created if it is already in the pool. Would anyone explain why the output is not "true" instead?
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
I'll take a stab at it (certain to be supplemented/corrected by the real experts here).
I think this has to do with compiler optimization. If the s4 assignment line had been
String s4 = "abcd";
then I think the output would be true.
But it seems that while the compiler knows that "ab" is the same as "ab", it does not know that "ab" is the same as "a" + "b", at least when assigning references to Strings in the pool.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
I think Scott is on to something here. I believe that the computer doesn't know that the two strings in the pool "ab" and "cd" will make up the third ("abcd") when combined, thus it creates a new one. The only time the pool is checked is when you use a constant (i.e. acctually type "abcd"), not when variables are used.
/Mike
Kishore Pamu
Ranch Hand

Joined: Mar 18, 2001
Posts: 35
Hi!Kevin,
I modified ur code alittle bit.
public class Test {
public static void main (String [] argv)
{
String s1="ab";
String s2="abcd";
String s3="cd";
String s4=s1+s3; //new String
System.out.println(s2 == s4);//prints false
System.out.println(s2.equals(s4));//prints true
}
}
As strings are immutable when you add another string to
it ,it becomes a new string
trim(),substring(),toUpperCase(),concat() produces new strings.
Hope this helps!.
Output: False
RHE describes clearly that NO new string literal will be created if it is already in the pool. Would anyone explain why the output is not "true" instead?

------------------
Kishore


Kishore
Kishore Pamu
Ranch Hand

Joined: Mar 18, 2001
Posts: 35
Hi!Kevin,
I modified ur code alittle bit.
public class Test {
public static void main (String [] argv)
{
String s1="ab";
String s2="abcd";
String s3="cd";
String s4=s1+s3; //new String
System.out.println(s2 == s4);//prints false
System.out.println(s2.equals(s4));//prints true
}
}
As strings are immutable when you add another string to
it ,it becomes a new string
trim(),substring(),toUpperCase(),concat() produces new strings.
Hope this helps!.
Kevin Yip
Ranch Hand

Joined: Oct 17, 2000
Posts: 110
Hi Kishore,
Thanks for your response. I think Mike's answer is a convincing one. New String object is created if variables instead of string literals are used.
If we just add the keyword 'final' in the declaration of s1 and s3,
final String s1 = "ab";
final String s3 = "cd";
OR replace the line
String s4 = s1+s3;
with any one of the following:
String s4 = "" + "abcd";
String s4 = "a" + "bcd";
String s4 = "ab" + "cd";
String s4 = "abc" + "d";
etc.,
output will be 'true'. In all these cases there is no new String object created even Strings are immutable in general.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literal - strange problem
 
Similar Threads
Strings...
== with Strings is giving a different result
A question about String.
String puzzle
String concatenation