返回信息流代码:
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c == (a + b));
System.out.println(c.equals(a + b));
System.out.println(g == (a + b));
System.out.println(g.equals( a + b));
}
反编译代码:
Integer a = Integer.valueOf(1);
Integer b = Integer.valueOf(2);
Integer c = Integer.valueOf(3);
Integer d = Integer.valueOf(3);
Integer e = Integer.valueOf(321);
Integer f = Integer.valueOf(321);
Long g = Long.valueOf(3L);
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c.intValue() == a.intValue() + b.intValue());
System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
System.out.println(g.longValue() == a.intValue() + b.intValue());
System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
}
输出:
true
false
true
true
true
false
为什么第一个结果和第二个结果不一样?
这是一条镜像帖。来源:北邮人论坛 / java / #37920同步于 2015/1/5
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
来看段Java Integer的代码
icyfox
2015/1/5镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
public static Integer valueOf(int i)方法缓存-128到127之间的值
Integer i = 1 会调用valueOf(i)
用Integer i = new Integer(1)就没有这个问题
【 在 icyfox (【意涵团】狐狸|5号之前看完JVM) 的大作中提到: 】
: 代码:
: [code=java]
: public static void main(String[] args) {
: ...................
【 在 icyfox 的大作中提到: 】
: 代码:
: [code=java]
: public static void main(String[] args) {
: ...................
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
默认缓存-128~127的int
超过后就new
原来还有这么个类..
【 在 zlwmosquito (mosquito) 的大作中提到: 】
: public static Integer valueOf(int i) {
: assert IntegerCache.high >= 127;
: if (i >= IntegerCache.low && i <= IntegerCache.high)
: ...................
==判断两个对象是不是同一个对象。用.equals来判断值相等吧。
再从JLS摘几段很有意思的:
If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, the rule disallows any assumptions about the identity of the boxed values on the programmer's part. This allows (but does not require) sharing of some or all of these references. Notice that integer literals of type long are allowed, but not required, to be shared.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.
我想吐槽的是Java把实现的细节泄漏到语言的设计中了。
赞
【 在 nuanyangyang (暖羊羊) 的大作中提到: 】
: ==判断两个对象是不是同一个对象。用.equals来判断值相等吧。
: 再从JLS摘几段很有意思的:
: If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be th
: ...................