Java学习笔记16

Java笔记16

包装类

  • Java是面向对象的语言,但并不是“纯面向对象”的,因为我们经常用到的基本数据类型就不是对象。但是我们在实际应用中经常需要将基本数据转化成对象,以便于操作。比如:将基本数据类型存储到Object[]数组或集合中的操作等等。
  • 为了解决这个不足,Java在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类(Wrapper Class)。
  • 他们都位于java.lang包下,对应关系如下表:
基本数据类型对应的包装类
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean
  • 上面8个类,除了BooleanCharacter以外都是数字类型,他们都是抽象类java.lang.Number的子类。而Number类提供了intValue()longValue等一系列抽象方法,所以几个数字类的包装类都可以相互转型。
  • 包装类的基本作用:提供了基本数据类型,包装类,和字符串类型之间的数据转化。

装箱和拆箱

在这里插入图片描述

  • 手动装箱:
    • 方法一:通过new关键字调用Integer类的构造方法装箱
    • 方法二:通过valueOf()方法装箱(推荐)
  • 手动拆箱:
    • 使用xxxValue()方法拆箱,xxx代表基本数据类型,例如:intValue()doubleValue()
  • 在JDK1.5之前,必须手动装箱和拆箱。
  • JDK1.5之后,支持自动装箱和拆箱,也就是在进行基本数据类型和对应的包装类转换时,系统将自动进行,这将大大方便程序员的代码书写。

基本数据类型和字符串之间转换

  • 基本类型转成字符串:

    • 方法一:使用+号进行转换。Integer 类有一个静态的 toString() 方法,可以将整数转换为字符串。或者直接在整数后面加空字符串即可!
    • 方法二:使用Integer类中的toString()方法进行转换
  • 字符串转成基本类型:

    • 使用 包装类.parseXXX()方法进行转换,XXX代表基本数据类型,例如:Integer.parseInt()Boolean.parseBoolean()
  • 练习:

package com.clown.classes.others;

public class WrapperDemo01 {

    public static void main(String[] args) {

        System.out.println("==================装箱==================");

        //类型转换:装箱,基本数据类型转换为包装类(引用类型)

        int num = 10;

        //自动装箱:   JDK1.5之后的特性,使用了valueOf()方法  Integer.valueOf(num)
        Integer integer = num;

        //手动装箱:
        Integer integer1 = new Integer(num);  //方法一:通过 new关键字调用构造方法装箱
        Integer integer2 = Integer.valueOf(num);   //方法二:通过valueOf()方法装箱    推荐!!!

        System.out.println(integer+"\n"+integer1+"\n"+integer2);

        System.out.println("==================拆箱==================");

        //类型转换:拆箱,包装类(引用类型)转换为基本数据类型

        Integer integer3 = Integer.valueOf(100);

        //自动拆箱:   JDK1.5之后的特性
        int a = integer3;

        //手动拆箱:
        int b = integer3.intValue();

        System.out.println(a+"\n"+b);

        System.out.println("============基本类型转成字符串============");

        int n1 = 255;

        //1.使用 +号
        String s1 = n1 + "";
        //2.使用 Integer中的 toString()方法
        String s2 = Integer.toString(n1);
        String s3 = Integer.toString(n1,16);  //toString(int i, int radix)是toString()方法的一个重载方法,其中参数 int radix表示进制

        System.out.println(s1+"\n"+s2+"\n"+s3);

        System.out.println("============字符串转成基本类型============");

        String str1 = "150";  //注意保证类型兼容,否则抛出 NumberFormatException异常!
        String str2 = "true";

        //使用 包装类.parseXXX()方法   XXX代表基本数据类型~
        int n2 = Integer.parseInt(str1);
        boolean n3 = Boolean.parseBoolean(str2); //仅当输入字符串为 "true"时,输出 true,输入为其他非 "true"的字符串时,结果都为 false

        System.out.println(n2);
        System.out.println(n3);

    }

}

运行结果:

在这里插入图片描述

整数缓冲区

  • Java预先创建了256个常用的整数包装类型对象。
  • 在实际应用当中,对已创建的对象进行复用。
package com.clown.classes.others;

//整数缓冲区
public class WrapperDemo02 {

    public static void main(String[] args) {

        //1.
        Integer integer1 = new Integer(100);
        Integer integer2 = new Integer(100);
        System.out.println(integer1 == integer2);  //false

		/*
        基本数据类型中, == 比较的是数值。
        引用数据类型中, == 和 默认的 equals()方法比较的是地址值
        */

        //2.
        Integer integer3 = Integer.valueOf(100);  //自动装箱也是使用的 Integer.valueOf()方法
        Integer integer4 = Integer.valueOf(100);
        System.out.println(integer3 == integer4);  //true

        //3.
        Integer integer5 = Integer.valueOf(200);
        Integer integer6 = Integer.valueOf(200);
        System.out.println(integer5 == integer6);  //false

    }

}

先看运行结果:

在这里插入图片描述

发现:代码块1的运行结果为false代码块2的运行结果为true代码块3的运行结果为false

  • 探究原因:

代码块1的装箱过程可用下图表示:

在这里插入图片描述

可以看出对象integer1和对象integer2的地址不同,故输出false

查看valueOf()方法在Integer类中的源代码:

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

可以发现valueOf()方法实际上创建了一个IntegerCache(整数缓冲区),并将区间为[-128, 127]的整数都实例化保存在里面。当我们使用valueOf()方法进行装箱时,如果我们准备装箱的整数在区间[-128, 127]内,则直接复用缓冲区内已经实例化好的对象,如果准备装箱的整数不在区间[-128, 127]内,则使用new关键字实例化对象。

所以,代码块2代码块3可以这样理解:

代码块2

在这里插入图片描述

可以看出对象integer3和对象integer4的地址相同,故输出true

代码块3

在这里插入图片描述

可以看出对象integer5和对象integer6的地址不同,故输出false

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值