• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings...

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output displayed by the following program?
public class test
{
public static void main(String a[])
{
String s1 = "ab";
String s2 = "abcd";
String s5 = "abcd";
String s3 = "cd";
String s4 = s1 + s3;
s1 = s4;
System.out.print("s1 "+((s1 == s2)? "==" : "!=")+" s2\n");
System.out.print("s5 "+((s5 == s2)? "==" : "!=")+" s2");

}
}

I thought it should be s1 == s2 and s5 == s2. because all the 4 strings s1, s2,s4 and s5 point to the same literal "abcd". But I find that answer is s1 != s2. Can some one explain?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramya,
I see that your code says

How can "ab" be equal to "abcd" ?
Hope this helps.

Roopesh.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roopesh,
I guess you did not notice s1 = s4 on line10
public class test
{
public static void main(String a[])
{
String s1 = "ab";
String s2 = "abcd";
String s5 = "abcd";
String s3 = "cd";
String s4 = s1 + s3;
s1 = s4;
System.out.print("s1 "+((s1 == s2)? "==" : "!=")+" s2\n");
System.out.print("s5 "+((s5 == s2)? "==" : "!=")+" s2");

}
}
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How silly...my mistake...Sorry for that..

It is important to remember that Strings do not get interned (adding new strings in the String pool) either when using the keyword new or when two Strings are concatenated and atleast one of the operands is a variable.
Please see the code:


Output is
s1==s2: true
s1==s3: true
s1==s4: false
s1==s7: false

String s7 is a new String and not same as s1 since it uses "variables" s6 and s5.
Hope this helped and I have not left you more confused.

Roopesh.
 
Ranch Hand
Posts: 579
  • 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 a[])
{
String s1 = "ab";
String s2 = "abcd";
String s5 = "abcd";
String s3 = "cd";
String s4 = s1 + s3;
s1 = s4;
System.out.print("s1 "+((s1 == s2)? "==" : "!=")+" s2\n");
System.out.print("s5 "+((s5 == s2)? "==" : "!=")+" s2");

}
}
##############################################################
Hello Ms.Ramya
s1,s2,s5,s3 are compile time strings ,created in pool.but s4 is created at runtime where a stringbuffer is created then valueOf() method is called Then Again toString() Method is Called.
Runtime string 'll obviously 've Different Memory Location


Agrah Upadhyay
B.tech
3rd Year
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic