包装类的详解

  • Post author:
  • Post category:其他



目录


包装类的分类


包装类和基本数据的转换


包装类测试题


包装类和String类型的互相转换


Integer类和character类的常用方法


Integer创建机制


Integer习题


包装类的分类

1.针对八种基本数据类型相应的引用类型—包装类。

2.有了类的特点,就可以调用类中的方法。

基本数据类型 包装类
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

包装类和基本数据的转换

演示 包装类 和 基本数据类型的相互转换,

这里以int和Integer演示。

1)jdk5前的手动装箱和拆箱方式,装箱:基本类型->包装类型,反之,拆箱

2)jdk5以后(含jdk5)的自动装箱和拆箱方式

3)自动装箱底层调用的是valueOf方法,比如Integer.valueOf0

4)其它包装类的用法类似,不一 一举例

代码演示:

package idea.chapter13.wrapper;

/**
 * 演示自动装箱和拆箱
 */
public class Integer01 {
    public static void main(String[] args) {
        //演示int <--> Integer 的装箱和拆箱
        //jdk5前是手动装箱和拆箱
        //手动装箱 int->Integer
        int n1 = 100;
        //在jdk5以前只能使用这两种方法
        Integer integer = new Integer(n1);
        Integer integer1 = Integer.valueOf(n1);

        //手动拆箱
        //Integer -> int
        int i = integer.intValue();

        //jdk5后,就可以自动装箱和自动拆箱
        int n2 = 200;
        //自动装箱 int->Integer
        Integer integer2 = n2; //在底层会自动调用 Integer.valueOf(n2)
        //自动拆箱 Integer->int
        int n3 = integer2; //底层仍然使用的是 intValue()方法
    }
}

包装类测试题

代码演示:

package idea.chapter13.wrapper;

/**
 * 演示包装类的习题
 */
public class WrapperExercise01 {
    public static void main(String[] args) {
        //在地城
        Double d = 100d; //ok, 自动装箱 Double.valueOf(100d);
        Float f = 1.5f; //ok, 自动装箱 Float.valueOf(1.5f);

        Object obj1 = true ? new Integer(1) : new Double(2.0);
        //因为这是一个三元运算符(是一个整体) 所以即使new出的是一个Integer,但事实因为三元运算符是一个整体,所以
        // 最后的结果会自动提升为Double所以输出的是1.0
        System.out.println(obj1);//  1.0

        Object obj2;
        if (true)
            obj2 = new Integer(1);
        else
            obj2 = new Double(2.0);
        //因为if - else 两个是单独的因此执行完 obj2 = new Integer(1); 后 就直接继续往下执行,所以输出的是1 而不是1.0
        System.out.println(obj2);//1
        //输出什么 ? 1, 分别计算

    }
}

包装类和String类型的互相转换

以Integer和String转换为例 其他类似

代码演示:

package idea.chapter13.wrapper;

/**
 * 演示包装类和String演示
 */
public class WrapperVSString {
    public static void main(String[] args) {
        //包装类(Integer)->String
        Integer i = 100;//自动装箱 这里会有自动装箱,在底层会自动调用valueOf方法
        //Integer转成包装类的的三种方式
        //方式1
        String str1 = i + "";//在要转换的后面加上一个""就可以变成字符串,但是这个是以i为基础 创建的新的一个对象
        //方式2
        String str2 = i.toString();//调用toString方法
        //方式3
        String str3 = String.valueOf(i);

        //String -> 包装类(Integer)
        String str4 = "12345";
        Integer i2 = Integer.parseInt(str4);//使用到自动装箱  通过看源码指定返回的是一个int类型的数据所以相当于用一个Integer类型的变量去接收一个int类型的变量使用到了自动装箱
        //我们可以看到,parseInt方法返回的是一个int类型,我们直接使用Integer去接受,会有自动装箱的过程
        /*
            public static int parseInt(String s) throws NumberFormatException {
                return parseInt(s,10);
            }
         */
        
        //也可以使用构造器 该构造器可以接受一个String类型的参数
        /*
            public Integer(String s) throws NumberFormatException {
                this.value = parseInt(s, 10);
            }
         */
        Integer i3 = new Integer(str4);//构造器

        System.out.println("ok~~");

    }
}

Integer类和character类的常用方法

代码演示:

package idea.chapter13.wrapper;

/**
 * 演示Integer类和character类的常用方法
 */
public class WrapperMethod {
    public static void main(String[] args) {
        System.out.println(Integer.MIN_VALUE); //返回最小值
        System.out.println(Integer.MAX_VALUE);//返回最大值

        System.out.println(Character.isDigit('a'));//判断是不是数字
        System.out.println(Character.isLetter('a'));//判断是不是字母
        System.out.println(Character.isUpperCase('a'));//判断是不是大写
        System.out.println(Character.isLowerCase('a'));//判断是不是小写

        System.out.println(Character.isWhitespace('a'));//判断是不是空格
        System.out.println(Character.toUpperCase('a'));//转成大写
        System.out.println(Character.toLowerCase('A'));//转成小写

    }
}

Integer创建机制

我们看底层源码的时候,就知道,如果范围是在-128~ 127之间,那么值就是从catch数组中取的,

如果,不在这个范围当中,那么就是new 一个新的Integer

代码演示:

重点

主要是看范围 -128 ~ 127 就是直接返回

/*

//1. 如果i 在 IntegerCache.low(-128)~IntegerCache.high(127),就直接从数组返回

//2. 如果不在 -128~127,就直接 new Integer(i)

public static Integer valueOf(int i) {


if (i >= IntegerCache.low && i <= IntegerCache.high)

return IntegerCache.cache[i + (-IntegerCache.low)];

return new Integer(i);

}

*/

package idea.chapter13.wrapper;

public class WrapperExercise02 {
    public static void main(String[] args) {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j);  //False  //因为是new出来的对象,是两个不同的对象,因为==在判断引用类型的时候判断的是地址是否相同,所以返回的false
        
        
        //所以,这里主要是看范围 -128 ~ 127 就是直接返回
        /*
        //1. 如果i 在 IntegerCache.low(-128)~IntegerCache.high(127),就直接从数组返回
        //2. 如果不在 -128~127,就直接 new Integer(i)
         public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
         */
        
        //结论
        //如果范围在-128 ~ 127之间就不会new Integer(i);  如果不在,这个返回中,那么就会new 
        Integer m = 1; //底层 Integer.valueOf(1); -> 阅读源码
        Integer n = 1;//底层 Integer.valueOf(1);
        System.out.println(m == n); //T  在返回127~-128之间,是从catch这个数组中取得的所以返回true
        //所以,这里主要是看范围 -128 ~ 127 就是直接返回
        //,否则,就new Integer(xx);
        
        Integer x = 128;//底层Integer.valueOf(128);
        Integer y = 128;//底层Integer.valueOf(128);
        System.out.println(x == y);//False  因为超过了127~-128这个范围就是new一个新对象所以返回false
        //因为超过了这个范围,所以x和y是两个对象,==在判断引用类型的时候,判断的是地址是否相同,所以返回的是false

    }
}

Integer习题

思路分析:

示例一:因为是new出来的对象所以返回false 而且==在判断引用类型的时候,判断的是地址是否相同

示例二:因为是new出来的对象所以返回false 而且==在判断引用类型的时候,判断的是地址是否相同

示例三:因为在-128~127之间所以不需要new返回true

示例四:因为超过了-128~127范围就是重新new了所以返回false

示例五:上面是从catch数组获取的这个是重新new的所以返回false

示例六:只要有基本数据类型,判断的就是值是否相同

示例七:只要有基本数据类型,判断的就是值是否相同


注意:只有有基本数据类型,判断的是值是否相同

package idea.chapter13.wrapper;

public class WrapperExercise03 {
    public static void main(String[] args) {
        //示例一
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//F    因为是new出来的对象所以返回false  而且==在判断引用类型的时候,判断的是地址是否相同
        //示例二
        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);//F    因为是new出来的对象所以返回false  而且==在判断引用类型的时候,判断的是地址是否相同

        //示例三
        Integer i5 = 127;//底层Integer.valueOf(127)
        Integer i6 = 127;//-128~127
        System.out.println(i5 == i6); //T   因为在-128~127之间所以不需要new返回true
        //示例四
        Integer i7 = 128;
        Integer i8 = 128;
        System.out.println(i7 == i8);//F    因为超过了-128~127范围就是重新new了所以返回false
        //示例五
        Integer i9 = 127; //Integer.valueOf(127)   上面是从catch数组获取的
        Integer i10 = new Integer(127);       //这个是重新new的所以返回false
        System.out.println(i9 == i10);//F

        //示例六
        Integer i11 = 127;
        int i12 = 127;
        //只有有基本数据类型,判断的是
        //值是否相同
        System.out.println(i11 == i12); //T
        //示例七
        Integer i13 = 128;
        int i14 = 128;
        System.out.println(i13 == i14);//T
    }
}



版权声明:本文为weixin_53616401原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。