public class TestInteger{
/*知识点:封装类,自动装箱与自动拆箱,数据池*/
public static void main(String []args){
//实验1
int a=128;
int b=128;
System.out.println(a==b);
//实验2
int a1=-129;
int b1=-129;
boolean iden1=a1==b1;
System.out.println(iden1);
//实验3
Integer a3=128;
Integer b3=128;
boolean iden3=a3==b3;
System.out.println(iden3);
//实验4
Integer a4=-129;
Integer b4=-129;
boolean iden4=a4==b4;
System.out.println(iden4);
//实验5
Integer a5=127;
Integer b5=127;
boolean iden5=a5==b5;
System.out.println(iden5);
//实验6
Integer a6=-128;
Integer b6=-128;
boolean iden6=a6==b6;
System.out.println(iden6);
/*结论:由以上实验数据,可知在java中,有个数据池,其范围为(-128~127)。
用Integer类声明的对象,其值在(-128~127)之间的存放在数字池中。
(1)在这个范围内:通过“==”比较的是值;
(2)超过这个范围:通过“==”比较的是对象的应用
(3)在jdk1.4之前,实验3~实验6的声明写法是错误的,jdk1.5之后实验3~实验6的声明写法是正确的
*/
System.out.println("*************************");
//实验7
Integer a7=new Integer(127);
Integer b7=new Integer(127);
System.out.println(a7==b7);
//实验8:
Integer a8=new Integer(-128);
Integer b8=new Integer(-128);
System.out.println(a8==b8);
//结论:用new关键字创建的对象,通过“==”比较的是地址(引用)
//实验9:Integer类中的两个常用字段:最大值与最小值
//Long类中的两个常用字段:最大值与最小值
//public static final int MIN_VALUE
//public static final int MAX_VALUE
System.out.println("int类型的最大值"+Integer.MAX_VALUE);
System.out.println("int类型的最小值"+Integer.MIN_VALUE);
System.out.println("long类型的最小值"+Long.MIN_VALUE);
System.out.println("long类型的最小值"+Long.MIN_VALUE);
//实验10:如何将字符串类型的数字转换成基本类型:2种方法
String str="100";//字符串类型的数字
//法一:调用方法public static int parseInt(String s) throws NumberFormatException
//作用:将字符串参数作为有符号的十进制整数进行分析。
int aa=Integer.parseInt(str);
System.out.println(aa);
//法二:用构造器public Integer(String s)throws NumberFormatException
//作用:构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
//正好可以使用对基数为 10 的值使用的 parseInt 方法将该字符串转换成 int 值。
int bb=new Integer(str);
System.out.println(bb);
//实验11:自动装箱与自动拆箱
//自动装箱:基本类型的值直接赋给对象类型
Integer m=-200;
System.out.println(m);
//自动拆箱:对象类型的值直接赋给基本类型
int h=new Integer(129);
System.out.println(h);
}
}
封装类:即8种基本类型对应8种封装类
最新推荐文章于 2024-08-08 09:02:44 发布