public static void main(String[] args){
Integer a1 = 100;
Integer b1 = a1;//另一种也可以b1=100
Field field = null;
try {
field = a1.getClass().getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(true);
try {
field.set(a1, 5000);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("b1="+b1);
Integer c1 = 100;
System.out.println("c1="+c1);
}
public static void main(String[] args){
Integer a1 = 200;
Integer b1 = a1;
Field field = null;
try {
field = a1.getClass().getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(true);
try {
field.set(a1, 5000);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("b1="+b1);
Integer c1 = 200;
System.out.println("c1="+c1);
}
public static void main(String[] args){
Integer a1 = new Integer(100);
Integer b1 = a1;
Field field = null;
try {
field = a1.getClass().getDeclaredField("value");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(true);
try {
field.set(a1, 5000);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("b1="+b1);
Integer c1 = 100;
System.out.println("c1="+c1);
}
基本类型和包装类的对应 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
public class Demo_Integer {
public static void main(String[] args) {
//JDK1.5之前
int a = 100;
Integer a1 = new Integer(a); //将基本数据类型包装成对象,装箱
int b = a1.intValue(); //将对象转换为基本数据类型,拆箱
//JDK1.5之后
int x = 100;
Integer x1 = x; //自动装箱,把基本数据类型转换为对象
int y = x1 + x; //自动拆箱,把对象转换为基本数据类型
}
}
public class Demo_Integer {
public static void main(String[] args) {
Integer a = null;
int b = a + 100; //自动拆箱底层将会调用a.intValue(),a为null,自然会抛出 NullPointerException
System.out.println(b);
}
}
public class Demo_Integer {
public static void main(String[] args) {
Integer i1 = new Integer(97);
Integer i2 = new Integer(97);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = new Integer(197);
Integer i4 = new Integer(197);
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
}
}
Output: false true ----------- false true -----------
public class Demo_Integer {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
}
}
Output: true true ----------- false true -----------
public final class Integer extends Number implements Comparable<Integer> {
public static Integer valueOf(int i) {
//当 i >= -128 且 i <= 127 时,会直接将取缓冲区中的对象
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);//超过了byte取值范围会在堆内存创建
}
//内部类充当缓冲区
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
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);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有