各位大侠:
小弟在看<<thinking in java 4th>>的时候,里面有个例子: Integer int1 = new Integer(47);
Integer int2 = new Integer(47);
System.out.println(int1.equals(int2));结果为true。
这是为什么呢?int1和int2指向2个不同的对象啊,不应该等啊。请大虾们不吝指教啊!想不通
小弟在看<<thinking in java 4th>>的时候,里面有个例子: Integer int1 = new Integer(47);
Integer int2 = new Integer(47);
System.out.println(int1.equals(int2));结果为true。
这是为什么呢?int1和int2指向2个不同的对象啊,不应该等啊。请大虾们不吝指教啊!想不通
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
看看equals与=的区别
没,主要是我看错符号。。
你可以试下int1==int2的结果
Integer int2 = 47;
System.out.println(int1==int2);
System.out.println(int1 == int2);结果false;
所以我说:int1,int2是2个不同的对象,不该等啊
equals比较的是2个对象的什么?
虽然int1和int2是不同的对象,但他们存储的值一样,而equals就是比较的这个值
如果你想要得到false的结果,就用int1==int2
==不是判断对象是否相等,是判断两个对象所引用的内存地址是否是相同的。
object 里 equals 里边就是写的 ==
String里不同是因为重写了这个方法。
楼主这个问题主要是自动解箱导致的。
private final int value;
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
public int intValue() {
return value;
}上面是Integer中equals方法的实现,Integer的值存储在private变量value中,int1和int2是不同对象,但int1.value和int2.value都是一样的,都是47,而equals比较的就是value,而不是int1和int2本身
public class Integer{
private final int value;
public Integer(int value) {
this.value = value;
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
}这个是 API 里的代码
相信楼主现在知道为什么了吧
而没有重写equals方法的,就用使用Object的equals方法,比较的是地址。
所以这里的int1,int2比较的是值,是相等的。
是这样的吗?
objec里 equals 方法里边就是简单的用了下 ==/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The <code>equals</code> method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* <code>x</code>, <code>x.equals(x)</code> should return
* <code>true</code>.
* <li>It is <i>symmetric</i>: for any non-null reference values
* <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
* should return <code>true</code> if and only if
* <code>y.equals(x)</code> returns <code>true</code>.
* <li>It is <i>transitive</i>: for any non-null reference values
* <code>x</code>, <code>y</code>, and <code>z</code>, if
* <code>x.equals(y)</code> returns <code>true</code> and
* <code>y.equals(z)</code> returns <code>true</code>, then
* <code>x.equals(z)</code> should return <code>true</code>.
* <li>It is <i>consistent</i>: for any non-null reference values
* <code>x</code> and <code>y</code>, multiple invocations of
* <tt>x.equals(y)</tt> consistently return <code>true</code>
* or consistently return <code>false</code>, provided no
* information used in <code>equals</code> comparisons on the
* objects is modified.
* <li>For any non-null reference value <code>x</code>,
* <code>x.equals(null)</code> should return <code>false</code>.
* </ul>
* <p>
* The <tt>equals</tt> method for class <code>Object</code> implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values <code>x</code> and
* <code>y</code>, this method returns <code>true</code> if and only
* if <code>x</code> and <code>y</code> refer to the same object
* (<code>x == y</code> has the value <code>true</code>).
* <p>
* Note that it is generally necessary to override the <tt>hashCode</tt>
* method whenever this method is overridden, so as to maintain the
* general contract for the <tt>hashCode</tt> method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return <code>true</code> if this object is the same as the obj
* argument; <code>false</code> otherwise.
* @see #hashCode()
* @see java.util.Hashtable
*/
public boolean equals(Object obj) {
return (this == obj);
}
将所有的属性相同的对象作为同一个对象,而外部不同的被设计成外部方法,比如在windows文件夹对象,不可能都分配内存空间,而是将不同的位置作为外部行为作为外部状态,相同的作为内部属性.所以在-127到127之间的Integer的比较实际是同一个对象.如果你超过了127或者小于-127他们就不相等了,因为这是比较不常用的对象,jdk会为他分配内存空间.