一、String 对比 == 和 equals,具体描述如下
"=="操作符的作用
1)用于基本数据类型的比较,如下:
byte(字节) 8 -128 - 127 0
shot(短整型) 16 -32768 - 32768 0
int(整型) 32 -2147483648-2147483648 0
long(长整型) 64 -9233372036854477808-9233372036854477808 0
float(浮点型) 32 -3.40292347E+38-3.40292347E+38 0.0f
double(双精度) 64 -1.79769313486231570E+308-1.79769313486231570E+308 0.0d
char(字符型) 16 ‘ \u0000 - u\ffff ’ ‘\u0000 ’
boolean(布尔型) 1 true/false false
2)判断引用是否指向堆内存的同一块地址。
equals所在位置:
在Object类当中,而Object是所有类的父类,包含在jdk里面,但并不适合绝大多数场景,通常需要重写
public boolean equals(Object obj) {
return (this == obj);
}
equals的作用:
用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同,返回值为布尔类型
二、Long 对比 == 和 equals,具体描述如下
Long相对来讲是一个比较特殊的,先说下面的例子:
例子1:
/**
* <一句话功能简述> <功能详细描述>
* @author xutianlong
* @param args
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
Long long1 = 128L;
Long long2 = 128L;
if (long1 == long2)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
if (long1.equals(long2))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
输出:false 和true
例子2:
/**
* <一句话功能简述> <功能详细描述>
* @author xutianlong
* @param args
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
Long long1 = 1L;
Long long2 = 1L;
if (long1 == long2)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
if (long1.equals(long2))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
输出:true和true
可能很多人觉得这是一件很神奇的事,其实不然,这个需要读JDK的源码才能知道原因,找到源码
Long的里面有一个私有的静态内部类,实现如下:
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);
}
显而易见,-128到127直接的值都放在cache里,不会创建新的对象,所以==比较的时候,结果是正确的,
当超过这个范围,会创建的新对象,所以不会相等
三、Integer类型也是同理与Long类型,可以查看到Integer实现的时候也是存在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) {
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);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
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);
}