Re: equals vs ==

From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 12/18/04


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


Relevant Pages

  • Re: Catching invalid string comparisons
    ... the programmer expected it to check if it is equal to an empty string. ... compares for equality -- but two references are equal ... Comparing the objects themselves, ... requires use of the equals() method. ...
    (comp.lang.java.programmer)
  • Re: about an assembly language
    ... >;Absolute address references should be avoided until the image has been> relocated and the MMU enabled. ... > Your "MOV R2,#CODEINRAM" is based on the assumption that pc equals to ... > Applied Data Systems ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Question about "Contains" method
    ... It's the contained type the one who needs to override Equals. ... > Magsood's comment about comparing references only ... For reference types, equality is defined as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java compatibility issues (WAS: MF having issues?)
    ... I suspect you are correct, but it's tricker than you are implying. ... because it can detect the same literal being used twice, and only instantiate a single String object for both the "a" and "b" references. ... In fact, I believe, that the JLS (Java Language Specification) REQUIRES that this optimization be done. ... The method being invoked is called "equals", but there's nothing to prevent the programmer from implementing the equals method from, for example, printing out the first 20 fibonacci numbers to the standard console. ...
    (comp.lang.cobol)
  • Re: InetAddress.equals() Anomoly?
    ... Compares this object against the specified object. ... array components is the same for the byte arrays. ... > If you consider that two InetAddress should be equal as soon as they have ... If you start overriding equals ...
    (microsoft.public.dotnet.vjsharp)