浪人与酒
2020-11-29
275浏览量
判等问题,在我们代码中就是一句话的事情,但是这一行代码如果处理不好,不仅会出现致命的bug,下面我们就以Java中 equals、compareTo 和 Java 的数值缓存、字符串驻留等问题展开讨论
在业务代码中,我们通常使用 equals 或 == 进行判等操作。equals是方法而 ==是操作符
: 1.对基本类型,比如 int 、long、进行判断,只能使用 == ,比较对是直接值,因为基本类型对值就是其数值
: 2.对引用类型,比如Integer 、Long 和 String 进行判等,需要使用 equals 进行内容判等。因为引用类型,需要使用equals进行内容判等。因为饮用类型等直接值是指针,使用 == 的话,比较的是指针,也就是两个对象在内存中的地址,即比较他们是不是同一个对象,而不是比较对象内容
public static void main(String[] args) throws Exception {
Integer a = 127;
Integer b = 127;
System.out.println(" a == b " +(a == b));
Integer c = 128;
Integer d = 128;
System.out.println(" c == d " + (c == d));
Integer g = new Integer(127);
Integer h = new Integer(127);
System.out.println(" g == h " + (g == h));
Integer i = 128;
int j = 128;
System.out.println(" i == j " + (i == j));
}
结果 :
a == b true
c == d false
g == h false
i == j true
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
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;
}
如果看过 Object 类源码,你可能就知道,equals 的实现其实是比较对象引用:
public boolean equals(Object obj) {
return (this == obj);
}
之所以 Integer 或 String 能通过 equals 实现内容判等,是因为它们都重写了这个方法。
String 的 equals 的实现:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
Integer.equals.()
/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
个人博客地址:http://blog.yanxiaolong.cn/
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
集结各类场景实战经验,助你开发运维畅行无忧