Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

String comparison with "==" is not reliable?

Thread view: 
www - 29 Nov 2007 18:42 GMT
Hi,

I always thought I can use "==" to compare if two Strings are equal(with
same case too). I know there is a method .equals(Object o) or
equalsIgnoreCase(String s). But I always thought "==" is good enough.

This morning, I found out that I was wrong.

I have two Strings, str1 and str2. They received values from some where
else. When I print them out to screen, they look same.(I am aware of
some unvisible thing, so I used trim()

//str1 and str2
if(str1.trim() == str2.trim())
{
    System.out.println("equal");
}
else
{
    System.out.println("Not equal");
}

Above code print out "Not equal". But the following code print out "equal".

if(str1.trim().equalsIgnoreCase(str2.trim()))
{
    System.out.println("equal");
}
else
{
    System.out.println("Not equal");
}

Can you give me some hint? Thank you.
Jason Cavett - 29 Nov 2007 19:02 GMT
> Hi,
>
[quoted text clipped - 32 lines]
>
> Can you give me some hint? Thank you.

== is reliable, but not for what you're trying to do.  == compares the
object's references.  .equals() compares the actual string value
(which is what most people are trying to do).  This is the case with
any object.  AFAIK, the only time == actually compares values is if
you're comparing primitives (ints, double, floats).
Lew - 30 Nov 2007 01:49 GMT
> == is reliable, but not for what you're trying to do.  == compares the
> object's references.  .equals() compares the actual string value
> (which is what most people are trying to do).  This is the case with
> any object.  AFAIK, the only time == actually compares values is if
> you're comparing primitives (ints, double, floats).

Not quite.  By default, equals() compares references, not values of the
objects themselves ("string" or otherwise).  Only if equals() is overridden
does its behavior change, and then to that of the override, which is entirely
up to the programmer.

In the case of String, equals() has been overridden to compare the chars in
the String value for equality.  In the case of Integer, equals() has been
overridden to compare the wrapped int values for equality.  Other classes
override equals() differently, or not at all.  If they do not override
equals(), it behaves just like ==.

If you override equals(), you had better override hashCode(), and vice versa.

Signature

Lew

Joshua Cranmer - 29 Nov 2007 22:16 GMT
> Hi,
>
> I always thought I can use "==" to compare if two Strings are equal(with
> same case too). I know there is a method .equals(Object o) or
> equalsIgnoreCase(String s). But I always thought "==" is good enough.

== is equals in the sense of "they point to the same object." There are
only three times you can guarantee this with Strings:

1. String literals are equal.
2. Internal String representations (i.e., str1.intern()) are equal.
  2a. This will print true (i.e., the intern of a String literal is
itself):
public class StringTest {
    public final static void main(String... args) {
        String str1 = "hello";
        String str2 = new String(str1);
        System.out.println(str1 == str2.intern());
    }
}
3. The two objects were assigned to each other or to a common object at
some point in time.

Any time a String does not meet any of these two requirements the equals
sign will return false.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.