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