题目Demo.java
public class Demo{
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);//1
System.out.println(e==f);//2
System.out.println(c==(a+b));//3
System.out.println(c.equals(a+b));//4
System.out.println(g==(a+b));//5
System.out.println(g.equals(a+b));//6
}
}
运行结果:
true
false
true
true
true
false
复制代码
结果分析:
1为true,2为false:查看Integer.valueOf方法,因为IntegerCache.cache默认会缓存-128到127的值,321不在该范围,所以2为false。
4为true,6为false:查看Long.equals方法,因为g和a、b类型不同,所以为false
复制代码
javac之后变为Demo.class 通过JD-GUI反编译之后
import java.io.PrintStream;
public class Demo
{
public static void main(String[] paramArrayOfString)
{
Integer localInteger1 = Integer.valueOf(1);
Integer localInteger2 = Integer.valueOf(2);
Integer localInteger3 = Integer.valueOf(3);
Integer localInteger4 = Integer.valueOf(3);
Integer localInteger5 = Integer.valueOf(321);
Integer localInteger6 = Integer.valueOf(321);
Long localLong = Long.valueOf(3L);
System.out.println(localInteger3 == localInteger4);
System.out.println(localInteger5 == localInteger6);
System.out.println(localInteger3.intValue() == localInteger1.intValue() + localInteger2.intValue());
System.out.println(localInteger3.equals(Integer.valueOf(localInteger1.intValue() + localInteger2.intValue())));
System.out.println(localLong.longValue() == localInteger1.intValue() + localInteger2.intValue());
System.out.println(localLong.equals(Integer.valueOf(localInteger1.intValue() + localInteger2.intValue())));
}
}
复制代码
以下是涉及到的源码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
字节码
0 iconst_1
1 invokestatic #2 <java/lang/Integer.valueOf>
4 astore_1
5 iconst_2
6 invokestatic #2 <java/lang/Integer.valueOf>
9 astore_2
10 iconst_3
11 invokestatic #2 <java/lang/Integer.valueOf>
14 astore_3
15 iconst_3
16 invokestatic #2 <java/lang/Integer.valueOf>
19 astore 4
21 sipush 321
24 invokestatic #2 <java/lang/Integer.valueOf>
27 astore 5
29 sipush 321
32 invokestatic #2 <java/lang/Integer.valueOf>
35 astore 6
37 ldc2_w #3 <3>
40 invokestatic #5 <java/lang/Long.valueOf>
43 astore 7
45 getstatic #6 <java/lang/System.out>
48 aload_3
49 aload 4
51 if_acmpne 58 (+7)
54 iconst_1
55 goto 59 (+4)
58 iconst_0
59 invokevirtual #7 <java/io/PrintStream.println>
62 getstatic #6 <java/lang/System.out>
65 aload 5
67 aload 6
69 if_acmpne 76 (+7)
72 iconst_1
73 goto 77 (+4)
76 iconst_0
77 invokevirtual #7 <java/io/PrintStream.println>
80 getstatic #6 <java/lang/System.out>
83 aload_3
84 invokevirtual #8 <java/lang/Integer.intValue>
87 aload_1
88 invokevirtual #8 <java/lang/Integer.intValue>
91 aload_2
92 invokevirtual #8 <java/lang/Integer.intValue>
95 iadd
96 if_icmpne 103 (+7)
99 iconst_1
100 goto 104 (+4)
103 iconst_0
104 invokevirtual #7 <java/io/PrintStream.println>
107 getstatic #6 <java/lang/System.out>
110 aload_3
111 aload_1
112 invokevirtual #8 <java/lang/Integer.intValue>
115 aload_2
116 invokevirtual #8 <java/lang/Integer.intValue>
119 iadd
120 invokestatic #2 <java/lang/Integer.valueOf>
123 invokevirtual #9 <java/lang/Integer.equals>
126 invokevirtual #7 <java/io/PrintStream.println>
129 getstatic #6 <java/lang/System.out>
132 aload 7
134 invokevirtual #10 <java/lang/Long.longValue>
137 aload_1
138 invokevirtual #8 <java/lang/Integer.intValue>
141 aload_2
142 invokevirtual #8 <java/lang/Integer.intValue>
145 iadd
146 i2l
147 lcmp
148 ifne 155 (+7)
151 iconst_1
152 goto 156 (+4)
155 iconst_0
156 invokevirtual #7 <java/io/PrintStream.println>
159 getstatic #6 <java/lang/System.out>
162 aload 7
164 aload_1
165 invokevirtual #8 <java/lang/Integer.intValue>
168 aload_2
169 invokevirtual #8 <java/lang/Integer.intValue>
172 iadd
173 invokestatic #2 <java/lang/Integer.valueOf>
176 invokevirtual #11 <java/lang/Long.equals>
179 invokevirtual #7 <java/io/PrintStream.println>
182 iconst_5
183 anewarray #12 <java/lang/Integer>
186 dup
187 iconst_0
188 iconst_1
189 invokestatic #2 <java/lang/Integer.valueOf>
192 aastore
193 dup
194 iconst_1
195 iconst_2
196 invokestatic #2 <java/lang/Integer.valueOf>
199 aastore
200 dup
201 iconst_2
202 iconst_3
203 invokestatic #2 <java/lang/Integer.valueOf>
206 aastore
207 dup
208 iconst_3
209 iconst_4
210 invokestatic #2 <java/lang/Integer.valueOf>
213 aastore
214 dup
215 iconst_4
216 iconst_5
217 invokestatic #2 <java/lang/Integer.valueOf>
220 aastore
221 invokestatic #13 <java/util/Arrays.asList>
224 astore 8
226 iconst_0
227 istore 9
229 iload 9
231 aload 8
233 invokeinterface #14 <java/util/List.size> count 1
238 if_icmpge 262 (+24)
241 getstatic #6 <java/lang/System.out>
244 aload 8
246 iload 9
248 invokeinterface #15 <java/util/List.get> count 2
253 invokevirtual #16 <java/io/PrintStream.println>
256 iinc 9 by 1
259 goto 229 (-30)
262 aload 8
264 invokeinterface #17 <java/util/List.iterator> count 1
269 astore 9
271 aload 9
273 invokeinterface #18 <java/util/Iterator.hasNext> count 1
278 ifeq 304 (+26)
281 aload 9
283 invokeinterface #19 <java/util/Iterator.next> count 1
288 checkcast #12 <java/lang/Integer>
291 astore 10
293 getstatic #6 <java/lang/System.out>
296 aload 10
298 invokevirtual #16 <java/io/PrintStream.println>
301 goto 271 (-30)
304 aload 8
306 invokeinterface #20 <java/util/List.stream> count 1
311 getstatic #6 <java/lang/System.out>
314 dup
315 invokevirtual #21 <java/lang/Object.getClass>
318 pop
319 invokedynamic #22 <accept, BootstrapMethods #0>
324 invokeinterface #23 <java/util/stream/Stream.forEach> count 2
329 return
复制代码