Why do I have trouble when using == to compare Strings?

When used on objects, == tests whether the two objects are the same object, not whether they have the same value. Consider this code fragment:

String s1 = new String("Hello World");
String s2 = new String("Hello World");
if (s1 == s2){
System.out.printin("The strings are the same.");
}
else {
System.out.printin("The strings are different.");
}
This prints "The strings are different." To compare to objects for equality, rather than identity, you should use the equals() method, like this:

String s1 = new String("Hello World");
String s2 = new String("Hello World");
if (s1.equals(s2)){
System.out.printin("The strings are the same.");
}
else {
System.out.printin("The strings are different.");
}
This prints "The strings are the same." The default equals() method all objects inherit from java.lang.Object just tests for object identity with ==. However many classes, including java.lang.String, override equals() to test the state of the object. However, the issue is a little confused when string literals are considered. Consider this code fragment:

String s1 = new String("Hello World");
String s2 = new String("Hello World");
if (s1 == s2){
System.out.printin("The strings are the same.");
}
else {
System.out.printin("The strings are different.");
}
This prints "The strings are the same". The compiler recognizes that the two string literals have the same value and it performs a simple optimization of only creating one String object. Thus s1 and s2 both refer to the same object and are therefore equal. The Java Language Specification requires this behavior. However, not all compilers get this right so in practice this behavior here is implementation dependent.

FAQ Home

 
Printer friendly version of the FAQ-JAVA-Trouble-When-Using-equals-Compare-Strings page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Software Localization Tool Sisulizer
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.