commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <GGreg...@seagullsoftware.com>
Subject RE: [lang] equalsBuilder unexplained wrong equality with java.lang.Longs ?
Date Mon, 17 Dec 2007 09:32:44 GMT
Hello Laurent:

This behavior is 'normal'. The concepts of object equality (the equals() method) and object
identity (==) are different. The method ' equals()' defined in Object can be overridden by
subclasses and defines if an object is 'equal' to another. Long overrides the equals method
to check to see if the object passed in wraps the same primitive long value. OTOH, the ==
operator compares two object identities, that is, is the object on the left-hand side the
same object as the one on the right-hand side. Even though two objects instances may be semantically
equal (as in the example below), they are not the same object so fails the == comparison.

For example:

public class TestLongEquals extends TestCase {

    public void testname() throws Exception {
        Long long1a = new Long(1);
        Long long1b = new Long(1);
        Assert.assertFalse(long1a == long1b);
        Assert.assertTrue(long1a.equals(long1b));
    }
}

Gary
Seagull Software

> -----Original Message-----
> From: Laurent Perez [mailto:hakimm@gmail.com]
> Sent: Sunday, December 16, 2007 3:17 PM
> To: Jakarta Commons Users List
> Subject: [lang] equalsBuilder unexplained wrong equality with java.lang.Longs ?
>
> Hi !
>
> I'm struggling with a problem I don't understand, my equalsBuilder's
> isEquals seem to return false when dealing with Longs :
>
> I have a bean class UiTheme, with a property private Long id.
>
> The following equals builder will return false when this.id = 1 and
> rhs.id =  1 :
>
>         public boolean equals(Object object) {
>                 if (!(object instanceof UiTheme)) {
>                         return false;
>                 }
>                 UiTheme rhs = (UiTheme) object;
>                 return new
> EqualsBuilder().appendSuper(super.equals(object)).append(this.id,
> rhs.id).isEquals();
>         }
>
> If I manually do the equality, as in this.id.equals(rhs.id), then it
> is true (because, well... 1 = 1 !)
> If I do an equality like this.id == rhs.id, then it is false (being a
> Java newbie... maybe it should be false, not sure if doing == instead
> of equals(obj) is correct with Longs)
>
> So I assume (am I wrong ?) that equalsBuilder's isEquals uses "=="
> instead of "equals(obj)"... but I really, really don't understand why.
>
> Can anyone enlighten me ?
>
> thanks !
> --
> <a href="http://in-pocket.blogspot.com">http://in-pocket.blogspot.com
> - Mobile world, technology and more</a>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message