包装类

import org.junit.Test;

/**
 * @author xianyu
 * @time 2019-08-11-13:09
 */
public class demo {

    @Test
    public void test1() {

        //包装类
        Long l1 = 128L;

        Long l2 = 128L;

        System.out.println(l1 == l2); //1  false

        System.out.println(l1 == 128L); //2  true

        Long l3 = 127L;

        Long l4 = 127L;

        System.out.println(l3 == l4);  //3  true

        System.out.println(l3 == 127L);  //4  true


        Integer i1 = new Integer(127);   //手动装箱

        Integer i2 = new Integer(127);   //手动装箱

        System.out.println(i1 == i2); //false   对于两边都是包装类型的比较 == 比较的是引用,equals 比较的是值

        System.out.println(i1.equals(i2)); //true


        Integer i3 = new Integer(128);

        Integer i4 = new Integer(128);

        System.out.println(i3 == i4); //false

        System.out.println(i3.equals(i4)); //true


        Integer i5 = 128;   // 自动装箱,调用valueOf(128)函数

        Integer i6 = 128;  // 自动装箱,调用valueOf(128)函数

        System.out.println(i5 == i6); //false

        System.out.println(i5.equals(i6)); //true


        Integer i7 = 127;   // 自动装箱,调用valueOf(127)函数

        Integer i8 = 127;  // 自动装箱,调用valueOf(127)函数


        System.out.println(i7 == i8); //true

        System.out.println(i7.equals(i8)); //true


        int i9 = i5;    //自动拆箱

        int i10 = i6.intValue();   //手动拆箱

        System.out.println(i9 == i10);   //true

    }

    @Test
    public void test2() {

        Boolean b1 = false;  //valueOf(false)

        Boolean b2 = false;  //valueOf(false)

        Boolean b3 = true;   //valueOf(true)

        Boolean b4 = true;  //valueOf(true)

        System.out.println(b1 == b2); //true

        System.out.println(b3 == b4); //true


        // Double 类型与 Integer 类型一样会调用到 Double 的 valueOf 方法,
        // 但是 Double 的区别在于不管传入的参数值是多少都会 new 一个对象来表达该数值
        // (因为在指定范围内浮点型数据个数是不确定的,整型等个数是确定的,所以可以 Cache

        Double d1 = 100.0;

        Double d2 = 100.0;

        Double d3 = 200.0;

        Double d4 = 200.0;

        System.out.println(d1 == d2); //false

        System.out.println(d3 == d4); //false

        System.out.println(d1);   //100.0
        //拆箱
        double d5 = d1.doubleValue();


    }

    @Test
    public void test3() {

        //java中基本数据类型与字符串类型之间相互转换

        //基本数据类型 ------> 字符串类型  三种方法
        /*
         *    1、将基本数据类型与空字符串(" ")连接(+)即可获得其所对应的字符串
         *    2、调用String 类中的valueOf()方法返回相应字符串
         *    3、使用包装类的toString()方法
         *
         * */

        int a = 23;

        String str1 = a + "";
        String str2 = String.valueOf(a);
        String str3 = Integer.toString(a);

    }


    /*
     * 字符串类型转 -------> 基本数据类型:
     *1、调用基本数据类型对应的包装类中的方法parseXXX(String)
     *
     *
     * */

    String str4 = "234";
    int b = Integer.parseInt(str4);
    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值