2021-04-05

通常情况下基本数据类型的变量不是对象,为了满足万物皆对象的理念就需要对基本数据类型进行封装打包处理变成对象,而负责将这些变量声明为成员变量进行对象化处理的相关类,叫做包装类。例如:
Person p = new person();
int num = 10;
public class Myint {
private int num = 10;
}

package task11;

public class IntegerTest {
public static void main(String[] args) {
//1.d打印Integer类中常用的常量数值
System.out.println(“最大值是:”+ Integer.MAX_VALUE);
System.out.println(“最小值是:”+ Integer.MIN_VALUE);
System.out.println(“所表示的二进制位数是:”+ Integer.SIZE);
System.out.println(“所占字节的个数是:”+ Integer.BYTES);
System.out.println(“对应int类型的Class实例是:”+ Integer.TYPE);
System.out.println("++++++++++++++++++++");
//2.使用构造方法来构造Integer类型的对象并打印
Integer it1 = new Integer(123);
System.out.println(“it1=”+it1);//自动调用它内部已经重写的toString方法 123
Integer it2 = new Integer(456);
System.out.println(“it2=”+it2);//自动调用它内部已经重写的toString方法 456
System.out.println("----------------------------");
//上述方法已经过时,建议使用valueOf方法取代,相当于从int到integer类型的转化,叫做 装箱(打包)
Integer it3 = Integer.valueOf(123);
System.out.println(“it3=”+it3);
//相当于从String到Integer类型的转换
Integer it4 = Integer.valueOf(“456”);
System.out.println(“it4=”+it4); // it4自动调用toString方法得到的是String类型
//获取调用对象中的整数数值,相当于从Integer类型到int类型的转换,叫做 拆箱
int ia = it4.intValue();
System.out.println(“获取到的整数数据是:”+ia);//456 ia得到的是整数类型,拼接完还是字符串
System.out.println("----------------------------");
//3.从java5开始增加了自动装箱和拆箱机制
Integer it5 = 100; //直接通过赋值运算符实现自动装箱
//之前是 Integer it5 = Integer.valueOf(“100”);
int ib = it5; //直接通过赋值运算符实现自动拆箱
//之前是 int ib = it5.intValue();
System.out.println("----------------------------");
//笔试考点
Integer it6 = 128;
Integer it7 = 128;
Integer it8 = new Integer(128);
Integer it9 = new Integer(128);
System.out.println(it6it7); //比较地址 false
System.out.println(it6.equals(it7));//比较内容 true
System.out.println(it8
it9); //比较地址 false
System.out.println(it8.equals(it9));//比较内容 true
//在Integer类的内部提供了自动装箱池技术,将-128~127之间的整数已经装箱完毕,
// 当程序中使用该范围之内的整数时,无需装箱直接取用自动装箱池中的对象即可,从而提高效率。
//因此下面的it10和it11来自同一个池子,所以其地址也是一样的。
Integer it10 = 127;
Integer it11 = 127;
Integer it12 = new Integer(127);
Integer it13 = new Integer(127);
System.out.println(it10it11); //比较地址 true
System.out.println(it10.equals(it11));//比较内容 true
System.out.println(it12
it13); //比较地址 false
System.out.println(it12.equals(it13));//比较内容 true
System.out.println("----------------------------");
//5.实现静态方法的调用
int ic = Integer.parseInt(“200”);
System.out.println(“字符串转换为整数的结果是:”+ic);//200
//int id = Integer.parseInt(“200a”);
//System.out.println(“字符串转换为整数的结果是:”+id);//数字格式异常,不能有字母
//算数异常 下标越界异常 空指针异常 类型转换异常 数字格式异常
System.out.println(“根据参数指定的整数获取对应的十进制字符串是:”+Integer.toString(200));
System.out.println(“根据参数指定的整数获取对应的二进制字符串是:”+Integer.toBinaryString(200));//11001000
System.out.println(“根据参数指定的整数获取对应的十六进制字符串是:”+Integer.toHexString(200));//c8
System.out.println(“根据参数指定的整数获取对应的八进制字符串是:”+Integer.toOctalString(200));//310
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值