2-Integer创建机制
0
Word Count: 411(words)
Read Count: 2(minutes)
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
public class WrapperExercise02 { public static void main(String[] args) { Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i == j);
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
Integer m = 1; Integer n = 1; System.out.println(m == n);
Integer x = 128; Integer y = 128; System.out.println(x == y); } }
|
源码中 integercache.cache在类加载时完成了加载
二、面试题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| 看看下面代码,输出什么结果.WrapperExercise03.java 5min package com.hspedu.wrapper;
public class WrapperExercise03 { public static void main(String[] args) {
Integer i1 = new Integer(127); Integer i2 = new Integer(127); System.out.println(i1 == i2);
Integer i3 = new Integer(128); Integer i4 = new Integer(128); System.out.println(i3 == i4);
Integer i5 = 127; Integer i6 = 127; System.out.println(i5 == i6);
Integer i7 = 128; Integer i8 = 128; System.out.println(i7 == i8);
Integer i9 = 127; Integer i10 = new Integer(127); System.out.println(i9 == i10);
Integer i11=127; int i12=127; =========================================== 只要有基本数据类型,判断的是 值是否相等。 这里 i11 自动调用了 intValue 方法 System.out.println(i11==i12);
Integer i13=128; int i14=128; System.out.println(i13==i14); } }
|