Re: equals vs ==
From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 12/18/04
- Next message: Phil Staite: "Re: equals vs =="
- Previous message: Tony Morris: "Re: sort method for a 2D object array?"
- In reply to: <- Chameleon ->: "equals vs =="
- Next in thread: Phil Staite: "Re: equals vs =="
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 18 Dec 2004 21:43:57 GMT
"<- Chameleon ->" <cham_gss@hotmail.nospam.com> scribbled the following:
> if (s instanceof String) {
> if (s == "a string");
> if (s.equals("a string"));
> }
> what is the difference between s.equals and s == ???
> When I use s == "something", many times if s == "something", returns false!
> Instead s.equals works correct always.
> Is there something that I dont know?
== compares identity, equals() compares equality. Two object references
will only ever return true from == if they refer to the *EXACT* *SAME*
object. "Exact same" meaning there being only one object, with two
references referring to it. OTOH, equals() will return also true if the
references refer to two objects that are considered equal. The
consideration depends on the object class. It defaults to being the
same as ==. String overrides it by comparing the contents of the string.
Other objects can override it in other ways.
-- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-------------------------------------------------------- rules! --------/ "We sorcerers don't like to eat our words, so to say." - Sparrowhawk
- Next message: Phil Staite: "Re: equals vs =="
- Previous message: Tony Morris: "Re: sort method for a 2D object array?"
- In reply to: <- Chameleon ->: "equals vs =="
- Next in thread: Phil Staite: "Re: equals vs =="
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|