java包装类详解

一、包装类存在的意义

             1、作为和基本数据类型对应的类型存在,方便涉及到对象的操作,如Object[]、集合(在泛型中,基本类型是不可以做泛型参数的)等的操作。

             2、可以使这个类型具有很多可以调用的方法(如:基本数据类型、包装类对象、字符串相互之间的转换)

             3、可以使每种基本数据类型具有一些属性可以操作(如最大值、最小值等)

二、八种包装类型

基本数据类型包装类型包装类的父类
bytejava.lang.Bytejava.lang.Number
shortjava.lang.Short
intjava.lang.Integer
longjava.lang.Long
floatjava.lang.Float
doublejava.lang.Double
booleanjava.lang.Booleanjava.lang.Object
charjava.lang.Character

 

 

 

 

 

 

 

 

 

Number类是一个抽象类,Number类的父类是Object。

 

三、自动装箱、自动拆箱

       自动装箱:Java自动将基本数据类型值转换成对应的对象,比如将int的变量转换成Integer对象,这个过程叫做装箱

       自动拆箱:Java自动将包装类对象转换成基本数据类型值,比如Integer对象转换成int类型值,这个过程叫做拆箱

              当 "=="运算符的两个操作数都是包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)
四、包装类的缓存值

           java中为了提高程序的执行效率,会将[-128,127]之间所有的包装对象提前创建好放到了方法区的整数型常量池当中进行了缓存,

          所以两个非new生成的Integer对象的值在区间[-128,127],用“==”比较结果为true,这是因为不会在堆中new对象,直接指向整数型常量池中已缓冲的对象,

          故这俩个对象的内存地址相同,所以用“==”比较结果为true。

          当超过[-128,127]范围后,会在堆中创建新的对象,所以两对象的内存地址不同,此时两对象之间比较不能再使用==进行数值的比较,而是使用equals方法来比较。
          Integer num2 = 100,Integer num3 = 100时,就直接从方法区的整数型常量池的缓存中取,不会new对象,所以使用==进行数值的比较结果为true。
          Integer num4 = 128,Integer num5 = 128时,会创建两个新的Integer(128),所以使用==进行数值的比较(比较的数值是两对象的内存地址)结果为false,使用equals方法来比较结果为true。

          

package wrapperclasstest;

public class WrapperTest02 {
    public static void main(String[] args) {

        
        //在堆中创建了新的对象,num1中保存的是堆中Integer对象的内存地址
        Integer num1 = new Integer(100);
        
        //不会在堆中创建新的对象,num2和num3中保存的都是方法区整数型常量池中缓存的那个Integer = 100的内存地址
        Integer num2 = 100;
        Integer num3 = 100;

        //会在堆中创建两个新的对象
        Integer num4 = 128;
        Integer num5 = 128;
        System.out.println(num1 == num2);//false
        System.out.println(num1 == num3);//false
        System.out.println(num2 == num3);//true
        System.out.println(num1.equals(num2));//true
        System.out.println(num4 == num5);//false
        System.out.println(num4.equals(num5));//true
    }
}

以上代码的内存图如下:

         

 

五、包装类和基本数据类型比较

               包装类Integer变量在与基本数据类型int变量比较时,Integer会自动拆包装为int,然后进行比较,实际上就是两个int变量进行比较

                

package wrapperclasstest;

public class WrapperTest02 {
    public static void main(String[] args) {

        Integer n1 = 128;
        int n2 = 128;
        System.out.println(n1==n2);//true
        int n3 = 1000;
        System.out.println(n1>n3);//false

    }
}

六、Integer常用方  

1、public Integer(int value)
2、public int intValue()
3、public static int parseInt(String s) throws NumberFormatException
    将字符串数字转化为int类型的数字
4、public static String toBinaryString(int i)
    将int类型的十进制数字转化为二进制,返回值是字符串形式的二进制
5、public static String toHexString(int i)
    将将int类型的十进制数字转化为十六进制,返回值是字符串形式的十六进制
6、public static String toOctalString(int i) 
    将int类型的十进制数字转化为八进制,返回值是字符串形式的八进制
7、public static Integer valueOf(int i)
    将int类型数字转化为Integer实例
8、public static Integer valueOf(String s) throws NumberFormatException
    将String类型数字转化为Integer实例
package wrapperclasstest;

public class WrapperTest03 {
    public static void main(String[] args) {
        //手动装箱
        Integer n1 = new Integer(100);

        //手动拆箱
        int n2 = n1.intValue();
        System.out.println(n2);

        //Integer的另一个构造方法
        Integer n3 = new Integer("123");
        System.out.println(n3);
        //以下代码编译期没问题,运行期会出现数字格式化异常NumberFormatException
        //Integer n4 = new Integer("西安");

        //静态方法,传参String,返回int
        int ret = Integer.parseInt("111");
        System.out.println(ret+89);//200

        //int十进制数字-->字符串形式的二进制
        System.out.println(Integer.toBinaryString(4));// 100

        //int类型的十进制数字-->字符串形式的十六进制
        System.out.println(Integer.toHexString(17));// 11

        //int类型的十进制数字-->字符串形式的八进制
        System.out.println(Integer.toOctalString(12));// 14

        //int类型的数字---->Integer实例
        Integer i1 = Integer.valueOf(123);
        System.out.println(i1);

        //String类型的数字---->Integer实例
        Integer i2 = Integer.valueOf("124");
        System.out.println(i2);
    }
}

七、Integer、int、String相互转换

package wrapperclasstest;

public class WrapperTest04 {
    public static void main(String[] args) {
        //String--->int
        int n1 = Integer.parseInt("12");
        System.out.println(n1+1);//13

        //String--->Integer
        Integer i1 = Integer.valueOf("12");
        System.out.println(i1+1);//13

        //int--->String
        String s1 = 12+"";
        System.out.println(s1+1);//121

        String s2 = String.valueOf(12);
        System.out.println(s2+0);//120

        //int--->Integer
        Integer i2 = Integer.valueOf(12);
        System.out.println(i2+1);//13

        //Integer--->int
        Integer i3 = Integer.valueOf("12");
        int i4 = i3.intValue();
        System.out.println(i4+1);//13
        
        //Integer--->String
        Integer i5 = Integer.valueOf(12);
        String s3 = String.valueOf(i5);
        System.out.println(s3+0);//120
    }
}

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值