• 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

Different behaviour with string objects

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The string objects behaving differently in differnt situations. Plase find the following code giving differnt result.

if("String".replace('t','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

/*************************************/

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

/*************************************/

if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

Please let me know why I am getting different results in the above three sinarios.

Thanks & Regards
Rajesh
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you want to compare the actual content of an object for equivalence you should use the method equals(). This is because although the contents of your Strings are the same, == checks that the object references are equivalent. Which is not the case in all three of your examples.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pandoo Go:

if("String".replace('t','t') == "String")



Here, you're asking "replace" to perform a null operation -- i.e., a replacement that wouldn't change the String. The standard implementation of replace() optimizes this case and simply returns the original String. Two identical literals always refer to the same object, so this will print "Equal".


if("String".replace('g','G') == "String".replace('g','G'))



Each call to replace() will create a new String. These two new Strings will have the same contents, but won't be the same physical object, so this will print "Not Equal".


if(" String ".trim() == "String")



The call to trim() will return a new String, which won't be the same object as the literal "String", so this is another "Not Equal".

As a previous poster noted, "==" tells you if two Strings are the same physical object in memory, while the equals() methods compares the contents and returns true as long as the two Strings contain the same characters in the same order.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic