• 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

String class

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written following pgm -
class string
{
public static void main(String a[])
{
String s1 = "Hello";
String s2= "o";
String s3= "Hell" + s2;
if (s1 == s3 )
{
System.out.println("Equal");
}
else
{
System.out.println("Not Equal");
}
}
}
As per my knowledge, It should print Equal but it is printing Not Equal. Reason I am teling you - What I think -
object s1 is created in the string pool and it's address is fixed.
object s2 is also created in the string pool and it's address is fixed also.
object s3 is also created in the string pool due to the concatenation of s1 and s2.Since the content of s3 is equal to s1,so s3 will be having the same address as of s1. since both the object s1 and s3 are pointing to the same memory location.So, as per me, Equal should be printed.
Please correct me where I am wrong.
Thanks
Rakesh Ranjan

------------------
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try to use equals insted, when you write == it gives s1 the string value of s3.

public class string
{
public static void main(String a[])
{
String s1 = "Hello";
String s2= "o";
String s3= "Hell" + s2;
if (s1.equals(s3))
{
System.out.println("Equal");
}
else
{
System.out.println("Not Equal");
}
}
}

------------------
Mettis
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Johan
I agree with you that equal method wll return True. But, What will be the result when I use == operator? Since both of them having same memory address, so it should print True.
Any one can clarify me.
Rakesh
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rakesh
You have nearly answered the question yourself. The reason you get 'Not Equal' is because, as you know, you are comparing the references not the values and thus you are being told the references are not equal.
So, why are the references not equal ? Because s3 is an entirely different String instance that just happens to have the same value as another String instance.
You want these to be the same instance, but think about what you are asking Java to do. Everytime it amends (i.e. creates a new) a String it would have to go through the string pool checking for equal instances! That's a lot of work, especially if the String you created was immediately modified again (in which case you should really be using StringBuffer but that's another topic...).
What you want to look at is the String intern() method, viz:


I hope that clarifies it.

Gary
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic