Re: String comparison with "==" is not reliable?
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 20:49:16 -0500
Jason Cavett wrote:
== 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.
--
Lew
.
- References:
- String comparison with "==" is not reliable?
- From: www
- Re: String comparison with "==" is not reliable?
- From: Jason Cavett
- String comparison with "==" is not reliable?
- Prev by Date: Re: Josephus Flavius Problem Help Please
- Next by Date: Re: Designation standard output as File type
- Previous by thread: Re: String comparison with "==" is not reliable?
- Next by thread: Re: String comparison with "==" is not reliable?
- Index(es):
Relevant Pages
|