String comparison with "==" is not reliable?

W

www

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.
 
J

Jason Cavett

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.

== 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).
 
J

Joshua Cranmer

www said:
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.
 
L

Lew

Jason said:
== 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top