directory-kerby mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel L├ęcharny <elecha...@gmail.com>
Subject eqlas() method
Date Fri, 08 Jan 2016 18:59:12 GMT
Hi guys,

I'm seeing a lot of equals() methods being implemented this way :

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        EncryptedData that = (EncryptedData) o;
        ...

This is not the best practice. Here is what should be used instead :


    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof EncryptedData) {
            return false;
        }

        EncryptedData that = (EncryptedData) o;
        ...

The (!(o instanceof <yourClass>)) test will always return false if 'o'
is not an instance of <yourClass> *and* when 'o' is null.




Mime
View raw message