Java学习历程十二《工具类之包装类》

包装类

  • 包装类?
  • 包装类和基本数据类型
  • 包装类的常用方法

回顾数据类型

  • 数据类型
    • 基本数据类型
      • 数值
        • 整数 byte short int long
        • 浮点 float double
      • 字符 char
      • 布尔 boolean
    • 引用数据类型
      • class
      • interface
      • array

基本数据类型和包装类的对应关系

基本数据类型,不具备对象特征,没有对象化的属性和方法

包装类可以让基本数据类型具有了,对象特征

基本数据类型对应包装类
byteByte
shortShot
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

包装类的常用方法

基本数据类型和包装类之间的转换

  • 装箱
    • 自动装箱
    • 手动装箱
  • 拆箱

自动装箱

package wrap;

public class WrapTestOne {
    public static void main(String[] args) {
        // 装箱操作
        // 1. 自动装箱
        int t1 = 2;
        Integer t2 = t1; // 自动将int类型的t1转换为Integer类型的t2
        System.out.println(t1);
        System.out.println(t2);
    }
}

手动装箱

package wrap;

public class WrapTestOne {
    public static void main(String[] args) {
        // 装箱操作
        // 2. 手动装箱
        int t1 = 10;
        Integer t3 = new Integer(t1);
        System.out.println(t1);
        System.out.println(t3);
    }
}

自动拆箱

package wrap;

public class WrapTestOne {
    public static void main(String[] args) {
        // 拆箱操作
        int t1 = 10;
        Integer t3 = new Integer(t1);
        // 1. 自动拆箱
        int t4 = t3;
        System.out.println(t1);
        System.out.println(t3);
        System.out.println(t4);
    }
}

手动拆箱

package wrap;

public class WrapTestOne {
    public static void main(String[] args) {
        // 拆箱操作
        int t1 = 10;
        Integer t3 = new Integer(t1);
        // 2. 手动拆箱
        int t5 = t3.intValue();
        // 可以通过包装类的方法来对数据进行转换
        double t6 = t3.doubleValue();
        System.out.println(t1);
        System.out.println(t3);
        System.out.println(t5);
        System.out.println(t6);
    }
}
基本数据类型和字符串之间的转换

基本数据类型转换成为字符串

package wrap;

public class WrapTestTwo {
    public static void main(String[] args) {
        // 将基本数据类型转为字符串
        int t1 = 1000;
        String t2 = Integer.toString(t1);
        System.out.println(t1 + t1);
        System.out.println(t2 + t2);
    }
}

字符串转换成为基本数据类型

package wrap;

public class WrapTestTwo {
    public static void main(String[] args) {
        // 将字符串转为基本数据类型
        // 方式一
        int t3 = Integer.parseInt(new String("1000"));
        System.out.println(t3+t3);
    }
}

package wrap;

public class WrapTestTwo {
    public static void main(String[] args) {
        // 将基本数据类型转为字符串
        // 方式二
        String st = "1000";
        int t1 = Integer.valueOf(st);
        /*
        赋值符号右侧,将String转换为Integer对象
        赋值符号左侧,Integer对象执行自动拆箱的操作,赋值给t1[int]
         */
        System.out.println(t1 + t1);
    }
}

小练习:

package wrap;

public class WrapTest {
    public static void main(String[] args) {
        // 使用两个new关键字来实现Integer对象的结果
        Integer one = new Integer(100);
        Integer two = new Integer(100);
        System.out.println(one==two);
    }
}

结果为false因为==用来判断两个对象是否相等的时候,判断的是内存地址,new 实现了
两个不同的内存地址.

package wrap;

public class WrapTest {
    public static void main(String[] args) {
        int s = 100;
        Integer three = s;
        Integer four = s;
        System.out.println(three==four); //true
        System.out.println(three==100); // true 装箱后拆箱,还是同一个
        System.out.println(100==four); // true
    }
}

通过同一个Int的自动装箱的方式,实现两个Integer对象后用==比较的结果是true,
这是应为,当执行Integer three/four = s;的时候,编译器的执行过程为:

1. Integer three = Integer.valueOf(s);
如果  -128<s<127, 范围内,那么就会创建对象池
2. Integer four = s;的时候会将上面对象池中的对象指向four
3. 那么three和four指向的是同一个对象
package wrap;

public class WrapTest {
    public static void main(String[] args) {
        // 超出范围后
        Integer five = 200;
        Integer six = 200;
        System.out.println(five==six); //false
    }
}

超出(-128,127)范围后会实例新的对象,而不是取用对象池中的对象

上述的概念就是对象常量池

但是存在特例:float,double是不使用对象常量池的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值