package com.test;
public class Test {
public static void main(String []args) {
Integer a = 100;//此处若使用new,则==值必为false
Integer b = 100;
System.out.println(a==b);//true
Integer c = 150;
Integer d = 150;
System.out.println(c==d);//false
}
}
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset]; //符合值范围时候,进入也创建好的静态IntergerCache,i+offset的值表示去取cache数组中那个下标的值
}
return new Integer(i); //当不符合-128 127值范围时候。记住用的:new,开辟新的内存空间,不属于IntergerCache管理区
}
private static class IntegerCache {
private IntegerCache(){}
static final Integer cache[] = new Integer[-(-128) + 127 + 1]; //开辟-128到127的内存区。有0的位置哦
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128); //为内存区的数组每个对象赋值
}
}
Integer a = 100;
a++;
//这边a++是新创建了一个对象,不是以前的对象。
public static void main(String []args) {
Integer a = 100;
Integer b = a;//此时b指针指向值为100的堆地址 即a的堆地址,a==b成立
a++;//此时a指向的值发生变化为101,a指针指向101的堆地址。而b任然指向100
System.out.println(a==b);//false
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有