阶段一/常用工具/包装类

目录

包装类与基本数据类型

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

基本数据类型与字符串的转换

比较== 、equals

小tips


包装类与基本数据类型

基本数据类型没有属性、没有方法,无法进行对象化交互

通过包装类可以让基本数据类型拥有属性、方法,可以对象化交互

包装类与基本数据类型,包装类属于引用类型,故其默认值均是null

  • byte -> Byte 
  • short -> Short
  • int -> Integer
  • long -> Long
  • float -> Float
  • double -> Double
  • char -> Character
  • boolean -> Boolean

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

装箱:基本数据类型 -> 包装类

拆箱: 包装类 -> 基本数据类型

分类:
从实现的角度而言,可以分为“手动拆装箱”与“自动拆装箱”的操作。

  • 手动装箱:可以结合类型的构造方法或者valueOf()方法实现
  • 手动拆箱:可以通过xxxValue()方法实现。
  • 自动装箱:把一个基本类型变量直接赋给对应的包装类型变量。
  • 自动拆箱:允许把包装类对象直接赋给对应的基本数据类型变量
public class WrapTestOne {
    public static void main(String[] args){
        // 装箱:把基本数据类型转换为包装类
        // 1、自动装箱
        int t1 = 2;
        Integer t2 = t1;
        // 2、手动装箱
        Integer t3 = new Integer(t1);
        Integer tt = Integer.valueOf(t1);
   
        // 拆箱: 把包装类转换为基本数据类型
        // 1、自动拆箱
        int t4 = t2;
        // 2、手动拆箱
        int t5 = t2.intValue();
        double d = t2.doubleValue();
}

基本数据类型与字符串的转换

基本类型转为字符串

  • 包装类的toString()方法,该方法是静态的,可以通过类直接调用

字符串转为基本类型

  • 包装类的parse方法
  • 包装类的valueOf方法
public class WrapTestTwo {
    public static void main(String[] args){
        // 基本数据类型转换为字符串
        int t1 = 2;
        String s2 = new Integer(t1).toString();
        String s3 = Integer.toString(t1);
        // 字符串转为基本数据类型
        int t3 = new Integer(s2).intValue();
        int t4 = Integer.parseInt(s2); // 包装类的pare方法
        int t5 = Integer.valueOf(s2); // 现将字符串转换为包装类,再利用包装类的自动拆箱
    }
}

比较== 、equals

  • 拆箱后的数据是基础数据类型。用 == 判断相等性,比较的都是数值,如果是字符,比较的是ASCII值
  • 装箱后如果用 == 比较对象的内存地址,除Double、Float外,如数据值在缓存区范围内,则相同;反之会重新生成对象,为不同。
    • Byte、Short、Integer、Long:缓存[-128,127]
    • Character:缓存[0,127]
    • Boolean:缓存true、false
  • 当类型相同,且数值相同时,返回true;反之,返回false。当比对方为基本数据类型时,会先进行自动装箱操作,后进行比较。 
public class WrapTestThree {
    public static void main(String[] args){
        Integer one = new Integer(100);
        Integer two = new Integer(100);
        // == 两边是对象的话,比较的是对象在内存的引用而不仅仅是里面的值
        // one 和 two均是new出来的,是两个不同的空间
        System.out.println("one == two的结果: " + (one == two)); // false
        int one_ = one;
        int two_ = two;
        System.out.println("one.equals(two)的结果: " + one.equals(two)); // true
        System.out.println("one_ == two_的结果: " + (one_ == two_)); // true
        //当比对方为基本数据类型时,会先进行自动装箱操作,后进行比较。       
        System.out.println("one.equals(two_)的结果: " + (one.equals(two_))); // true

        Integer three = 100; // 自动装箱, 这边会隐式调用new,因为之前100还存在在混存区
        // 自动拆箱,相当与两个int类型变量比较
        System.out.println("three == 100的结果: " + (three == 100)); // true

        Integer four = 100;
        // four 与 three相等是因为,
        // Integer four = 100;的自动装箱底层是Integer four = Integer.valueOf(100)
        // Java为了提高运行效率,在内存中提供了一个类似常量数组的缓存区(对象池),
        // 执行valueOf方法时,若其参数在[-128, 127]之间,会直接去缓存重看是否存在该对象
        // 若有,则直接产生,这就是three == four的原因(指向缓存区的同一块空间),若没有,则隐式的通过new 来实例化对象
        // 8个基本类型除了float和double之外都是可以用常量池的概念的
        System.out.println("three  == four的结果: " + (three == four)); // true

        Integer five = 200;
        System.out.println("five == 200的结果: " + (five == 200)); // true
        Integer six = 100;
        // 参数不在[-128, 127]之间,故没有缓存区的概念了,均是隐式调用new
        System.out.println("five == six的结果: " + (five == six)); // false

        Double d1 = Double.valueOf(100);
        System.out.println("d1 == 100的结果: " + (d1 == 100)); // true
        Double d2= Double.valueOf(100);
        System.out.println("d1 == d2的结果: " + (d1 == d2)); // false
        // Error:不可比较的类型: java.lang.Double和java.lang.Integer
//        System.out.println("d1 == four的结果: " + (d1== four)); 不同类型不可比,

    }
}

小tips

方法重载时,当方法以基本数据类型和其对应包装类作为方法重载的参数条件时,原则是:各回各家,各找各妈。

public class WrapTestFour {
    public static void main(String[] args){
        test(new Integer(1));
        test(1);
    }
    public static void test(Integer i){
        System.out.println("行参是包装类的test");
    }
}

输出

行参是包装类的test
行参是包装类的test

 

public class WrapTestFour {
    public static void main(String[] args){
        test(new Integer(1));
        test(1);
    }
    public static void test(Integer i){
        System.out.println("行参是包装类的test");
    }
    public static void test(int i){
        System.out.println("行参是int的test");
    }
}

输出:

行参是包装类的test
行参是int的test

 

参考:慕课网-Java 工程师

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值