==
VS equals()
==
- 基础类型:
==
比较的是值 - 引用类型:
==
比较的是对象的内存地址
equals()
equals()
只能比较引用类型,无法比较基础类型.equals()
方法在顶级父类Object
中,代码如下:
public boolean equals(Object obj) {
return (this == obj);
}
可以看出这个代码就是判断是否是同一对象.那么,当子类重写 equals()
往往都是将属性内容相同的对象认为是同一对象,
如果子类不直接或间接重写Object
的equals()
方法,那么调用的equals()
和==
相同.
String a = new String("aa"); // a 为一个引用
String b = new String("aa"); // b为另一个引用,对象的内容一样
String aa = "aa"; // 放在常量池中
String bb = "aa"; // 从常量池中查找
System.out.println(aa == bb);// true
System.out.println(a == b);// false
System.out.println(a.equals(b));// true
上面代码的String
重写了equals()
,代码如下:
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;
}
hashCode()
VS equals()
hashCode()
函数返回哈希码,确定该对象在哈希表中的索引位置.同样属于Object
类中,代码如下:
public native int hashCode();
,native调用C/C++,返回int
哈希码.利用哈希码能够快速检索出对象
如HashSet
/HashMap
会先调用hashCode()
,如果哈希码不同,直接判断对象不同,否则进行下面的equals()
操作,大大减少了equals()
的操作,提高执行速度.(C/C++本身比Java执行快,equals()执行也比hashCode()逻辑复杂)hashCode()
返回值相同也不能认为是同一对象,存在hash冲突- hashCode 相同,equals()为true才能认为是同一对象
为什么重写 equals()
时必须重写 hashCode()
方法?
- 正面:两对象相等,那么hashCode也相等,equals()也为true
- 反面:重写
equals()
,但是不重写hashCode()
,会导致equals(),判断是同一个对象,但是哈希码并不同 - 例子: 重写了
equals()
方法,不重写hashCode()
,同一名学生,添加到hashSet中时候,会添加两次,因为你只是通过属性内容判断的是否为同一对象!!!
包装类型的常量池
- Byte/Short/Integer/Long 这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据
- Character 创建了数值在 [0,127] 范围的缓存数据
- Boolean 直接true/false
源代码如下:
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
/**
* Returns a {@code Byte} instance representing the specified
* {@code byte} value.
* If a new {@code Byte} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Byte(byte)}, as this method is likely to yield
* significantly better space and time performance since
* all byte values are cached.
*
* @param b a byte value.
* @return a {@code Byte} instance representing {@code b}.
* @since 1.5
*/
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
/**
* Returns a {@code Short} instance representing the specified
* {@code short} value.
* If a new {@code Short} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Short(short)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param s a short value.
* @return a {@code Short} instance representing {@code s}.
* @since 1.5
*/
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
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() {}
}
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
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 LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
/**
* Returns a {@code Long} instance representing the specified
* {@code long} value.
* If a new {@code Long} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Long(long)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* Note that unlike the {@linkplain Integer#valueOf(int)
* corresponding method} in the {@code Integer} class, this method
* is <em>not</em> required to cache values within a particular
* range.
*
* @param l a long value.
* @return a {@code Long} instance representing {@code l}.
* @since 1.5
*/
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
上面的缓存代码可以完美解释下面的结果:
Integer a = 10;
Integer b = 10;
System.out.println(a == b);// 输出 true
Float c = 10f;
Float d = 10f;
System.out.println(c == d);// 输出 false
Double e = 1.2;
Double f = 1.2;
System.out.println(e == f);// 输出 false
Integer a = 10;
Integer b = new Integer(10);
System.out.println(a==b);// false
//Integer a = 10;=>Integer a=Integer.valueOf(10);使用常量池中的对象,Integer b = new Integer(10);创建新对象
自动装箱与拆箱
从字节码中,我们发现装箱其实就是调用了 包装类的valueOf()方法,拆箱其实就是调用了 xxxValue()方法。
- Integer i = 1 等价于 Integer i = Integer.valueOf(1)
- int n = i 等价于 int n = i.intValue();
- 如果频繁拆装箱的话,也会严重影响系统的性能。我们应该尽量避免不必要的拆装箱操作。