1.包装类
public class Wang {
public static void main(String[] args) {
//基本数据类型转成包装类对象;
Integer a=new Integer(3);
Integer b=Integer.valueOf(30);
//把包装类对象转成基本数据类型;
int c=b.intValue();
double d=b.doubleValue();
//把字符串转为包装类对象;
Integer f=new Integer("9999");
Integer g=Integer.parseInt("998");
//把包装类对象转成字符串;
String str=f.toString();//""+f
//常见的常量;
System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
}
}
2. 自动装箱与拆箱,缓存
public class Wang {
public static void main(String[] args) {
Integer a=18;//自动装箱:Integer a=Integer.valueOf(18);
int b=a;//自动拆箱:int b=a.intValue();
/*
Integer c=null;
int d=c;//会报错;因为自动调用了c.intValue();
*/
//缓存[-128,127]之间的数字;实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组;
//当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围内则直接从缓存数组中拿出来已经建好的对象;
//如果不在这个范围,则创建新的Integer 对象;
Integer in1=-128;
Integer in2=-128;
System.out.println(in1==in2);//true 因为-128在缓存范围内;
System.out.println(in1.equals(in2));//true
Integer in3=1234;
Integer in4=1234;
System.out.println(in3==in4);//false 因为1234不在缓存范围内
System.out.println(in3.equals(in4));//true
}
}