简介

如果没有重写 equals 方法, 相当于 == 比较, 即比较两个对象的地址是否相等.

如果是基本数据类型, 直接对值进行比较.

code

/**
 * Created by lee on 2021/6/28.
 */
public class equalTest {
    public static void main(String[] args) {
        System.out.println(1 == 1);
        System.out.println(1 == 2);
        System.out.println(159 == 159);
        Person1 a = new Person1(1);
        Person1 b = new Person1(1);
        System.out.println(a == b);
        Integer c = new Integer(100);
        Integer d = new Integer(100);
        System.out.println(c == d);
        int e = 1;
        int f = 1;
        System.out.println(e == f);
        Integer g = 200;
        Integer h = 200;
        System.out.println(g == h);
        int g1 = 200;
        int h1 = 200;
        System.out.println(g1 == h1);
    }
}

answer

true
false
true
false
false
true
false
true
posted on 2021-06-28 16:38  HDU李少帅  阅读(172)  评论(0编辑  收藏  举报