Java基本数据类型的#hashCode和#equals方法

1、Boolean

1.1.Boolean#hashCode

 public static int hashCode(boolean value) {
        return value ? 1231 : 1237;
 }

 所以true和false对应的哈希值分别为“1231”、“1237”

1.2.Boolean#equals

public boolean equals(Object obj) {
        if (obj instanceof Boolean) {
            return value == ((Boolean)obj).booleanValue();
        }
        return false;
}
public static void main(String args[]) {
        boolean a = true;
        Boolean b = new Boolean(true);
        Boolean c = new Boolean(true);
        System.out.println(b.equals(a));//true
        System.out.println(b.equals(c));//true
    }

2.Byte

2.1.#hashCode

public static int hashCode(byte value) {
        return (int)value;
    }

2.2.#equals

public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

3.Character

 

posted @ 2018-12-05 19:03  maxudong  阅读(899)  评论(0编辑  收藏  举报