equal

Discussion in 'Java' started by Jerry, May 27, 2005.

  1. Jerry

    Jerry Guest

    Does o1.equals(o2) imply o1==o2?
     
    Jerry, May 27, 2005
    #1
    1. Advertising

  2. Jerry

    Bob Guest

    Jerry wrote:

    > Does o1.equals(o2) imply o1==o2?


    No, not necessarily.

    a.equals(b) is a test for meaningful equivalence.

    a == b is a test for the memory address of a against the memory address
    of b. I.e. are a and b one object in memory?
    --
    Bob
     
    Bob, May 27, 2005
    #2
    1. Advertising

  3. Jerry

    Ross Bamford Guest

    On Thu, 2005-05-26 at 17:20 -0700, Jerry wrote:
    > Does o1.equals(o2) imply o1==o2?


    Try it:

    public static void main(String[] args) {
    String s1 = new String("A string");
    String s2 = new String("A string");
    System.out.println("equals : "+s1.equals(s2)); // prints 'true'
    System.out.println("identity: "+(s1 == s2)); // prints 'false'
    }

    So, no.

    As an aside, if you use string literals for this then you will get see
    true for both println statements, which demonstrates the string pooling
    used by the JVM. You can see the same effect by 'internalising' the
    strings as you create them.

    String s1 = new String("A string").intern();
    String s2 = new String("A string").intern();

    s1 and s2 will reference the same object.

    --
    [Ross A. Bamford] [ross AT the.website.domain]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + in
     
    Ross Bamford, May 27, 2005
    #3
  4. Jerry

    Harald Guest

    (Jerry) writes:

    > Does o1.equals(o2) imply o1==o2?


    No, not at all: just because two things looks the same (equals()) they
    need not be identical (==).

    The other way round should better be true, however. Buggy
    implementations of equals() may exist though.

    Harald.

    --
    ---------------------+---------------------------------------------
    Harald Kirsch (@home)|
    Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
     
    Harald, May 29, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Vecchi

    Need to make control equal to control

    Ron Vecchi, Dec 11, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    442
    Jacob Yang [MSFT]
    Dec 12, 2003
  2. Ralph Krausse

    ALL 'try/catch/finally' NOT created equal?

    Ralph Krausse, Aug 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    423
  3. Ron de Waard
    Replies:
    2
    Views:
    12,593
    HansDampf
    Oct 5, 2007
  4. Emilio
    Replies:
    0
    Views:
    804
    Emilio
    Jul 12, 2004
  5. Jeroen

    Struts <logic:equal>

    Jeroen, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    1,027
    Jeroen
    Jul 17, 2004
Loading...

Share This Page