Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.



Tags
#bloch-effective-java-2ed #equality #java
Question

Suppose we have a class that tries to cooperate with String. What is wrong with it?


public final class CaseInsensitiveString {

    private final String s;

    @Override public boolean equals(Object o) {
        if (o instanceof CaseInsensitiveString)
            return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
        if (o instanceof String)
            return s.equalsIgnoreCase((String) o);
       return false;
    }

    // rest of the code
}
Answer
It violates symmetry of equality - "BlaBla".equals(whatever) will do case-sensitive comparison, even if whatever is CaseInsensitiveString (in which case the result is false)

Tags
#bloch-effective-java-2ed #equality #java
Question

Suppose we have a class that tries to cooperate with String. What is wrong with it?


public final class CaseInsensitiveString {

    private final String s;

    @Override public boolean equals(Object o) {
        if (o instanceof CaseInsensitiveString)
            return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
        if (o instanceof String)
            return s.equalsIgnoreCase((String) o);
       return false;
    }

    // rest of the code
}
Answer
?

Tags
#bloch-effective-java-2ed #equality #java
Question

Suppose we have a class that tries to cooperate with String. What is wrong with it?


public final class CaseInsensitiveString {

    private final String s;

    @Override public boolean equals(Object o) {
        if (o instanceof CaseInsensitiveString)
            return s.equalsIgnoreCase(((CaseInsensitiveString) o).s);
        if (o instanceof String)
            return s.equalsIgnoreCase((String) o);
       return false;
    }

    // rest of the code
}
Answer
It violates symmetry of equality - "BlaBla".equals(whatever) will do case-sensitive comparison, even if whatever is CaseInsensitiveString (in which case the result is false)
If you want to change selection, open document below and click on "Move attachment"

pdf

owner: piotr.wasik - (no access) - Effective Java (Joshua Bloch), 2ed, p35

Summary

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

Details

No repetitions


Discussion

Do you want to join discussion? Click here to log in or create user.