-
基本数据类型和包装类比较, 只要实际值相同, ==比较为true(拆箱)
-
同类型包装类equals比较均为true(比较实际值)
-
同类型包装类==比较,仅有效范围内赋值比较为true, 其他均false(double和float包装类除外,参考valueOf实现)
-
不同类型包装类equals比较均为false
//Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
//范围内取缓存, 范围外new(-128~127)
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
//Double、Float的valueOf方法的实现是类似的。
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
//Boolean的valueOf方法的实现
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
public class TypeTest {
public static void main(String[] args) {
/**
* 基本数据类型和包装类比较, 只要实际值相同, ==为true
* 包装类:
* 包装类equals(下面源码)比较: 同类型 等价于 数字比较(实际值相同就true),不同类型均false
* public boolean equals(Object obj) {
* if (obj instanceof Long) {
* return value == ((Long)obj).longValue();
* }
* return false;
* }
*
* 同类型包装类
* 有效范围内
* 赋值比较,==、equals均true
* new比较,==均false, equals均true
* 赋值、new比较,==均false, equals均true
* 非有效范围内
* 赋值比较,==均false, equals均true
* new比较,==均false, equals均true
* 赋值、new比较,==均false, equals均true
* 不同类型包装类
* equals比较均false(不可以使用==比较)
*
* 总结:
* 基本数据类型和包装类比较, 只要实际值相同, ==为true(拆箱)
* 同类型包装类equals比较均为true(比较实际值)
* 同类型包装类==比较,仅有效范围内赋值比较为true, 其他均false
* 不同类型包装类equals比较均为false
*
* 编译时关键在于valueOf方法
*
* Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
* //范围内取缓存, 范围外new(-128~127)
* public static Integer valueOf(int i) {
* if (i >= IntegerCache.low && i <= IntegerCache.high)
* return IntegerCache.cache[i + (-IntegerCache.low)];
* return new Integer(i);
* }
*
* Double、Float的valueOf方法的实现是类似的。
* public static Double valueOf(String s) throws NumberFormatException {
* return new Double(parseDouble(s));
* }
*
* Boolean的valueOf方法的实现
* public static Boolean valueOf(boolean b) {
* return (b ? TRUE : FALSE);
* }
*
*
* Integer i = 100; 装箱 Integer i = Integer.valueOf(100);
* int x = i.intValue(); 拆箱
*/
/**
* 有效范围内
*/
int i = 6;
Integer i1 = 6; // 反编译: Integer i1 = Integer.valueOf(6);
Integer i2 = 6;
Integer i3 = i1;
Integer i4 = new Integer(6);
Integer i5 = new Integer(6);
Long l1 = 6L;
Double d1 = 6.0;
System.out.println("基本数据类型和“赋值”包装类型==比较:" + (i == i1));//true
System.out.println("基本数据类型和“new”包装类型==比较:" + (i == i4));//true
System.out.println("“赋值”包装类型==比较:" + (i1 == i2));//true
System.out.println("“赋值”包装类型equals比较:" + (i1.equals(i2)));//true
System.out.println("同“赋值”包装类型equals比较:" + (i1.equals(i3)));//true
System.out.println("“赋值”包装类型和“new”包装类型==比较:" + (i1 == i4));//false
System.out.println("“赋值”包装类型和“new”包装类型equals比较:" + (i1.equals(i4)));//true
System.out.println("“new”包装类型==比较:" + (i4 == i5));//false
System.out.println("“new”包装类型equals比较:" + (i4.equals(i5)));//true
System.out.println("不同包装类型equals比较:" + (l1.equals(i2)));//false
System.out.println("不同包装类型equals比较:" + (d1.equals(i5)));//false
/**
* 非有效范围内
*/
i = 128;
i1 = 128;
i2 = 128;
i3 = i1;
i4 = new Integer(128);
i5 = new Integer(128);
l1 = 128L;
d1 = 128.0;
System.out.println("基本数据类型和“赋值”包装类型==比较:" + (i == i1));//true
System.out.println("基本数据类型和“new”包装类型==比较:" + (i == i4));//true
System.out.println("“赋值”包装类型==比较:" + (i1 == i2));//false
System.out.println("“赋值”包装类型equals比较:" + (i1.equals(i2)));//true
System.out.println("同“赋值”包装类型equals比较:" + (i1.equals(i3)));//true
System.out.println("“赋值”包装类型和“new”包装类型==比较:" + (i1 == i4));//false
System.out.println("“赋值”包装类型和“new”包装类型equals比较:" + (i1.equals(i4)));//true
System.out.println("“new”包装类型==比较:" + (i4 == i5));//false
System.out.println("“new”包装类型equals比较:" + (i4.equals(i5)));//true
System.out.println("不同包装类型equals比较:" + (l1.equals(i2)));//false
System.out.println("不同包装类型equals比较:" + (d1.equals(i5)));//false
}
}
————————————————
版权声明:本文为CSDN博主「Lmondi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xinyu112/article/details/82698760