本文共 1502 字,大约阅读时间需要 5 分钟。
public class Demo { public static void main(String[] args) { Integer c = -128; Integer d = -128; System.out.println("c == d: " + (c == d)); System.out.println("c.equals(d): " + c.equals(d)); System.out.println("c.intValue() == d.intValue(): " + (c.intValue() == d.intValue())); System.out.println("Objects.equals(c, d): " + Objects.equals(c, d)); Integer e = 127; Integer f = 127; System.out.println("e == f: " + (e == f)); System.out.println("e.equals(f): " + e.equals(f)); System.out.println("e.intValue() == f.intValue(): " + (e.intValue() == f.intValue())); System.out.println("Objects.equals(e, f): " + Objects.equals(e, f)); Integer g = 128; Integer h = 128; System.out.println("g == h: " + (g == h)); System.out.println("g.equals(h): " + g.equals(h)); System.out.println("g.intValue() == h.intValue():" + (g.intValue() == h.intValue())); System.out.println("Objects.equals(g, h): " + Objects.equals(g, h)); }}
运行结果:
c == d: truec.equals(d): truec.intValue() == d.intValue(): trueObjects.equals(c, d): truee == f: truee.equals(f): truee.intValue() == f.intValue(): trueObjects.equals(e, f): trueg == h: falseg.equals(h): trueg.intValue() == h.intValue():trueObjects.equals(g, h): true
参考博客:
转载地址:http://eqtii.baihongyu.com/