您现在的位置是:主页 > news > 智慧旅游网站开发与设计/推广引流方法有哪些?
智慧旅游网站开发与设计/推广引流方法有哪些?
admin2025/6/21 18:26:57【news】
简介智慧旅游网站开发与设计,推广引流方法有哪些?,福州在线,办公装修设计Java面试题中有这么一个问题:1Integer in1 100;2Integer in2 100;3System.out.println(in1 in2);这个两个对象是相等的,就是地址一样,对象的地址一样这设计到Integer中缓存池的概念。Intege这个int的包装类存在一个缓存池,这个…
Java面试题中有这么一个问题:
1Integer in1 = 100;2Integer in2 = 100;3System.out.println(in1 == in2);
这个两个对象是相等的,就是地址一样,对象的地址一样这设计到Integer中缓存池的概念。
Intege这个int的包装类存在一个缓存池,这个缓存池中默认的是存放的是-128到127之间的Integer对象,如下:
1private static class IntegerCache { 2 static final int low = -128; 3 static final int high; 4 static final Integer cache[]; 5 6 static { 7 // high value may be configured by property 8 int h = 127; 9 //可以设置高位的数字,所以127 可以更大10 String integerCacheHighPropValue =11 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");12 if (integerCacheHighPropValue != null) {13 try {14 int i = parseInt(integerCacheHighPropValue);15 //如果设置的小于127,不会生效16 i = Math.max(i, 127);17 // Maximum array size is Integer.MAX_VALUE18 //此处的目的是避免数组越界,超过Integer最大值19 h = Math.min(i, Integer.MAX_VALUE - (-low) -1);20 } catch( NumberFormatException nfe) {21 // If the property cannot be parsed into an int, ignore it.22 }23 }24 high = h;2526 cache = new Integer[(high - low) + 1];27 int j = low;28 for(int k = 0; k < cache.length; k++)29 cache[k] = new Integer(j++);3031 // range [-128, 127] must be interned (JLS7 5.1.7)32 assert IntegerCache.high >= 127;33 }3435 private IntegerCache() {}36 }
Integer in1 = 100; 这种方式编译成class文件之后,调用的是 Integer.valueOf() 这个方法:
1 public static Integer valueOf(int i) {2 if (i >= IntegerCache.low && i <= IntegerCache.high)3 return IntegerCache.cache[i + (-IntegerCache.low)];4 return new Integer(i);5 }
如果在-128到127之间就取缓存池中的对象,所以in1 == in2 这两个的地址相等,如果超出了127。。。
如果需要更改这个缓存池的范围的话,需要更改虚拟机的参数:-XX:AutoBoxCacheMax=1000
例如Idea中设置的方式:
再比如: int int1 = 200, Integer int2 = 200;
这两个数值的比较是如何的呢? 结果是true。 因为基本类型和包装类型的比较,会将包装类转为基本类型比较,比较的就是真实的值而不是地址,所以true。
这个过程中概念叫做装箱和拆箱。 装箱的含义是将基本类型转为包装类型,拆箱的含义是包装类型转为基本类型。
Integer中,装箱的过程是调用Integer.valueOf() 方法转为包装类,拆箱的过程是调用intValue方法转为基本类型。
