• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

String literal - strange problem

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic