Java------常用类之包装类

包装类

什么是包装类

  • 基本数据类型所对应的引用数据类型
  • Object可统一所有数据,包装类的默认值是null

包装类对应

基本数据类型包装类型
buteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

类型转换与装箱、拆箱

装箱:基本数据类型转换为引用数据类型

拆箱:引用数据类型转换为基本数据类型

  • 8种包装类提供不同类型间的转换方式
    • Number父类中提供的6个共性方法
    • parseXXX()静态方法
package com.Innerclass.lesson05;

public class Demo01 {
    public static void main(String[] args) {
        int num = 10;

//        类型转换:装箱:基本类型转换为引用类型的过程
        int num1 = 18 ;
//        使用Integer类创建对象,构造函数Integer(int) 在第9版后已弃用
        Integer integer = new Integer(num1);
        Integer integer1 = Integer.valueOf(num1);
        System.out.println("======装箱======");
        System.out.println(integer);
        System.out.println(integer1);

//        拆箱
        Integer integer2 = new Integer(10);
        int num2 = integer2.intValue();
        System.out.println("======拆箱======");
        System.out.println(num2);
        System.out.println("======");
//        JDK1.5之后,提供自动装箱,拆箱
        int age = 21;
//        自动装箱
        Integer integer3 = age ;
        System.out.println("自动装箱:"+integer3);
//        自动拆箱
        int age2 = integer3;
        System.out.println("自动拆箱:"+age2);
    }
}

**注意:**JDK1.5之后,提供自动装箱,拆箱

运行结果:

======装箱======
18
18
======拆箱======
10
======
自动装箱:21
自动拆箱:21

基本类型与字符串之间转换

package com.Innerclass.lesson05;

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

//        基本类型和字符串之间转换
//        1.基本类型转成字符串
        int n1= 15 ;
//        1.1使用+号
        String s1 = n1+"";
//        1.2使用Integer中的toString()方法
        String s2 = Integer.toString(n1);
        String s3 = Integer.toString(n1,16);//将n1的值转换为十六进制的值
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println("==========");

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

//        boolean字符串形式转成基本类型:"true"---->true  非"true"----->false
        String str2 = "true";
        String str3 = "wanji";
        Boolean b1 = Boolean.parseBoolean(str2);
        Boolean b2 = Boolean.parseBoolean(str3);
        System.out.println(b1);
        System.out.println(b2);
    }
}

boolean字符串形式转成基本类型:“true”---->true 非"true"----->都为false

运行结果:

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

整数缓冲区

  • Java预先创建了256个常用的整数包装类型对象
  • 在实际应用当中,对已创建的对象进行复用
public class Demo02 {
    public static void main(String[] args) {
        Integer integer1 = new Integer(100);
        Integer integer2 = new Integer(100);
        System.out.println(integer1==integer2);
    }
}

运行结果:

false

因为==指向的是内存地址,integer1与integer2分别是两个对象,内存地址不一样,所以是false

public class Demo02 {
    public static void main(String[] args) {
    
        Integer integer3 = 100 ;//自动装箱
        Integer integer4 = 100 ;
        System.out.println(integer3==integer4);//true

        Integer integer5 = 200 ; //自动装箱
        Integer integer6 = 200 ;
        System.out.println(integer5 == integer6);//false
    }
}

运行结果:

true
false

因为Integer具有一个区间(-128,127),在判断值是否已经在这个区间创建之后,如果是则直接返回,如果不是在new一个对象,而200不再此区间内

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值