类型比较、装拆箱问题

  早上,在牛客看到这么一道关于类型比较与自动装拆箱的笔试题,于是做总结如下:

    @Test
    public void test6(){
        Integer i = 42;
        Long l = 42L;
        Double d = 42.0;
//        System.out.println(i==l);//报错Incompatible operand types Integer and Long
//        System.out.println(i==d);//报错Incompatible operand types Integer and Double
//        System.out.println(l==d);//报错Incompatible operand types Long and Double
        System.out.println(i.equals(d));
        System.out.println(d.equals(l));
        System.out.println(i.equals(l));
        System.out.println(l.equals(42L));
    }

结果:

false
false
false
true

解释:

前三个用==比较编译不通过,两边类型需要一致。

Integer.equals()方法查看源码发现首先比较类型,然后比较值,因此equals()比较不同类型的肯定是false:

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

考点:

1.对于引用类型的变量来说==比较的是地址,equals比较的是value

2.==是在同一类型的基本类型与包装类型比较会自动装拆包,equals不会对比较的数据类型进行转换。

类型比较、拆装箱总结:

1.两个均为包装类

  包装类型一致

    包装类型内容相等(输出true)

    包装类型内容不相等(输出false)

  包装类型不一致

    编译失败

2.一个包装类型一个非包装类型

  包装类和非包装类类型一致

    包装类内容相等(会拆箱,输出true)

    包装类内容不相等(会拆箱,输出false)

  包装类和非包装类类型不一致

    如果两类均为数字(如int、long、double、float、char=6) (输出true)

    如果一类为数字,一类为字符(char=6)  (输出false)

    如果一类为八大类型,另一类为String(编译失败)

3.两个均为非包装类型

  按照上面拆箱的结果

例如:对于Integer和int类型:

1、自动装箱:Integer iObj = 3;  装箱就是  自动将基本数据类型转换为包装器类型;  通过valueOf(int )实现,所有的valueOf(xxx)的返回类型都是Integer

装箱代码:

    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

2、自动拆箱:int n = iObj ;   //拆箱就是  自动将包装器类型转换为基本数据类型。

    装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的。(xxx代表对应的基本数据类型)。

拆箱代码:

    /**
     * Returns the value of this {@code Integer} as an
     * {@code int}.
     */
    public int intValue() {
        return value;
    }

注意:valueOf("123")在内部调用parseInt,parseInt返回类型是int基本数据类型

    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }

总结:

  parseInt(xxx) 和 valueOf(xxx)的区别:

  

   都是Integer的静态方法:(如果传入的是字符串处理的时候有可能出现NumberFormatException异常,只是是运行时异常所以不需要try。。。catch。。。)

Integer.parseInt(str)是把一个数字字符串转化成int类型的数据

Integer.valueOf(str or num) 是把一个数字或者一个字符串转化为Integer类型的数据;


//parseInt 会抛出异常NumberFormatException
public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }
 
//parseInt 这个源码太长就不贴了   有兴趣可自己百度
 public static int parseInt(String s, int radix)  throws NumberFormatException
    {
      
        return negative ? result : -result;
    }
 
//参数是数值类型的
 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
//参数是字符串类型的
 public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }


原文地址:https://www.cnblogs.com/qlqwjy/p/9181302.html