Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String literal - strange problem

 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Pamu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic