我们都知道,在java中“==”运算符比较的是对象在堆内存上的地址,如果要比较对象的值需要使用equals()方法。那么,对于基本类型我们该如何判断值是否相等呢?
1、我们先来看几个个示例:
示例1:基本类型的比较
//基本类型比较
public static void test1() {
int a = 21;
int b = 21;
if (a == b) {
System.out.println("a == b yes");
} else {
System.out.println("a == b no");
}
int c = 1270;
int d = 1270;
if (c == d) {
System.out.println("c == d yes");
} else {
System.out.println("c == d no");
}
}
输出:
a == b yes
c == d yes
示例2:包装类型之间的比较
public static void test2() {
Integer a = 21;
Integer b = 21;
if (a == b) {
System.out.println("a == b yes");
} else {
System.out.println("a == b no");
}
if (a.equals(b)) {
System.out.println("a.equals(b) yes");
}
if (a.intValue() == b.intValue()) {
System.out.println("a.intValue() == b.intValue() yes");
}
Integer c = 1270;
Integer d = 1270;
if (c == d) {
System.out.println("c == d yes");
} else {
System.out.println("c == d no");
}
if (c.equals(d)) {
System.out.println("c.equals(d) yes");
}
if (c.intValue() == d.intValue()) {
System.out.println("c.intValue() == d.intValue() yes");
}
}
输出:
a == b yes
a.equals(b) yes
a.intValue() == b.intValue() yes
c == d no
c.equals(d) yes
c.intValue() == d.intValue() yes
示例3:基本类型和包装类型的比较
public static void test3() {
Integer a = 21;
int b = 21;
if (a == b) {//自动拆箱
System.out.println("a == b yes");
} else {
System.out.println("a == b no");
}
if (a.equals(b)) {//自动装箱
System.out.println("a.equals(b) yes");
}
if (a.intValue() == b) {
System.out.println("a.intValue() == b.intValue() yes");
}
Integer c = 1270;
int d = 1270;
if (c == d) {
System.out.println("c == d yes");
} else {
System.out.println("c == d no");
}
if (c.equals(d)) {
System.out.println("c.equals(d) yes");
}
if (c.intValue() == d) {
System.out.println("c.intValue() == d.intValue() yes");
}
}
输出:
a == b yes
a.equals(b) yes
a.intValue() == b.intValue() yes
c == d yes
c.equals(d) yes
c.intValue() == d.intValue() yes
2、分析:
1)对于基本类型,由于不是对象,所以不存在地址,更没有equals等方法,故此只有使用“==”来进行值比较。
2)包装类:
Integer a = n; 这句话背后编译器实际上调用了valueOf()方法进行了自动装箱,我们看该代码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
这里做了一个判断,接下来看一下IntegerCache源码:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
到这里我们明白了,对于[-128,127]之间的数,jdk会将包装类对象做一个cache,所以我们可以使用”==“来比较对象的地址(地址都相同了,值肯定相同),超出了这个范围,就会new一个新的对象,所以在用”==“比较,肯定是不同的对象地址,返回false。
3)基本类型和包装类型之间的比较:
我们都知道,java对基本类型和包装类之间是可以通过自动装箱、拆箱转换的,所以,我们通过“==”运算符 或 equals方法都可以进行值比较。
- 自动装箱:编译器调用valueOf将基本类型值转换成包装类型;
- 自动拆箱:编译器通过调用类似intValue(),doubleValue()这类的方法将包装转换成基本原始类型值。
在示例3中:if (a == b) 实际上是通过了自动拆箱转换成了if (a.intValue() == b) ,进而转换成了基本类型的值比较;对于if (a.equals(b)) 实际上是通过自动装箱转换成了if (a.equals(Integer.valueOf(b))) ,进而转换成了包装类型的equals值比较。
3、结论:
- 由于常量池中的缓存,基本类型的包装类值在[-128,127] 期间,可以用 “==”进行值比较(Double、Float是没有缓存的),不在该范围内的需要使用equals进行值比较;(对于包装类,无论何时强烈推荐都使用equals进行值比较)
- 基本类型的包装类,也可以手动拆箱转成基本类型(例如:intValue()),再使用”==“进行值比较;
- 基本类型和基本类型的包装类之间的比较,可以直接使用”==“运算符进行值比较(jdk使用了自动拆箱),也可以使用equals方法进行值比较(jdk编译器使用了自动装箱);
- 对于基本类型,使用”==“运算符进行值比较;