Java SE 包装类

包装类

  • 基本数据类型所对应的引用数据类型
  • Object可统一所有数据,包装类的默认是null
  • 基本数据类型—>包装类型
    • byte—>Byte
    • short—>Short
    • int—>Integer
    • long—>Long
    • float—>Float
    • double—>Double
    • boolean—>Boolean
    • char—>Character

类型转换与装箱、拆箱

  • 基本类型转换为引用类型 装箱
  • 引用类型转换为基本类型 拆箱
  • 栈与堆
  • 8种包装类提供不同类型间的转换方式:
    • Number父类中提供的6个共性方法
    • parseXXX()静态方法 字符串

整数缓冲区

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

public class Demo01 {
    public static void main(String[] args) {
        //装箱:基本类型转换为引用类型
        int num1 = 24;
        Integer integer = new Integer(num1);
        Integer integer1 = Integer.valueOf(num1);
        System.out.println(integer);
        System.out.println(integer1);
        System.out.println("=============================");


        //拆箱:引用类型转换为基本类型
        Integer integer2 = new Integer(100);
        int num2 = integer2.intValue();

        System.out.println(num2);
        System.out.println("=============================");


        //自动装箱和拆箱
        int age = 18;
        Integer integer3 = age;
        int age1 = integer3;
        System.out.println(integer3);
        System.out.println(age1);

    }
}

结果:

24
24
=============================
100
=============================
18
18

Process finished with exit code 0

package BaoZhuang;

public class Demo02 {
    //基本类型与字符串之间的转换
    public static void main(String[] args) {

        //基本类型转换为字符串
        int n1 = 15;
        //第一种方法 +
        String s =n1+" ";
        //第二种方法 使用Integer中的toString方法
        String s1 = Integer.toString(n1);
        String s2 = Integer.toString(n1, 16);//16进制
        String s3 = String.valueOf(n1);
        // 二进制 Integer.toBinaryString();
        // 十六进制 Integer.toHexString();
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println("========================");

        //字符串转换为基本类型
        String str = "150";
        //使用Integer.parseXXX()
        int a = Integer.parseInt(str);
        System.out.println(a);


        //boolean字符串形式转成基本类型 "true"->true 非"true"->false

        String str2 = "true";
        boolean b = Boolean.parseBoolean(str2);
        System.out.println(b);


    }
}

15 
15
f
15
========================
150
true

Process finished with exit code 0

package BaoZhuang;

public class Demo03 {
    public static void main(String[] args) {
        //Integer缓冲区

        Integer integer1 = new Integer(100);
        Integer integer2 = new Integer(100);
        System.out.println(integer1==integer2);
        //false 推中的内存地址不一样?

        //Integer integer3 = 100; 自动装箱用的是valueOf()
        //Integer integer4 = 100;
        Integer integer3 = Integer.valueOf(100);
        Integer integer4 = Integer.valueOf(100);

        System.out.println(integer3==integer4);//true

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


    }
}

false
true
false

Process finished with exit code 0

原因:

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

-128—127

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值