Integer自动装箱/拆箱
作者:
我不懂我不懂a | 来源:发表于
2019-11-02 13:58 被阅读0次// 自动装箱 Integer a = Integer.valueOf(1);
// public static Integer valueOf(int i) {
// if (i >= IntegerCache.low && i <= IntegerCache.high)
// return IntegerCache.cache[i + (-IntegerCache.low)];
// return new Integer(i);
// }
// 保存的值在-128 127之间,则从缓存中取
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
//true
System.out.println(c == d);
//false
System.out.println(e == f);
//包装类的 “==”运算在不遇到算术运算的情况下不会自动拆箱
//false 正确:true
System.out.println(c == (a + b));
//true
System.out.println(c.equals(a + b));
//false 正确:true
System.out.println(g == (a + b));
//false
System.out.println(g.equals(a + b));
本文标题:Integer自动装箱/拆箱
本文链接:https://www.haomeiwen.com/subject/xencbctx.html
网友评论