• 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

I need help with Strings

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//This apparently produces:

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

//the code will compile an print "Equal".



//And this one

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

//the code will compile an print "Not Equal".

//Can any one explain it to me please:
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch. Well meaning folks will redirect you to the forum's naming policy.

Regarding your question, the behaviour is different because of sheer dumb luck. Consider yourself lucky, because it got you to ask this question and seek clarification.

The answer may or may not be easy to explain, but let me just say that you should use the 'equals' method (or perhaps the 'compareTo' method) to compare two strings, *never* the == operator.

- Anand
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Anand said, always use String.equals for comparing String contents.

The == operator (for non-primitives) returns true if both expressions point to he same object. Since Java pools Strings, the two expressions in your first example both point to the same object.
So the == symbol worked as expected but probably not for the reasons that you thought.

In your second example, you compared the contstant "String" with the result of the trim() method which returns a new instance of String. So.. even though the contents of the two strings match the two expressions did not return references to the same String object.

Try the same experiment using the .equals operator instead of the == operator.
[ January 26, 2008: Message edited by: Ben Souther ]
 
Manish Khurana
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks (i was tau17 before i got all those warnings)
reply
    Bookmark Topic Watch Topic
  • New Topic