我想澄清一下我的理解是否正确:
==是一个引用比较,即两个对象都指向相同的内存位置 .equals()计算为对象中值的比较
我想澄清一下我的理解是否正确:
==是一个引用比较,即两个对象都指向相同的内存位置 .equals()计算为对象中值的比较
当前回答
基本上,==比较两个对象在堆上是否有相同的引用,因此,除非两个引用链接到同一个对象,否则这种比较将为假。
equals()是继承自Object类的方法。默认情况下,如果两个对象具有相同的引用,则此方法进行比较。它的意思是:
object1.equals(object2) <=> object1 == object2
但是,如果您希望在同一类的两个对象之间建立相等性,则应该重写此方法。如果已经重写了equals(),重写hashCode()方法也是非常重要的。
当建立平等是Java对象契约的一部分时,实现hashCode()。如果你正在使用集合,并且你还没有实现hashCode(),可能会发生奇怪的糟糕事情:
HashMap<Cat, String> cats = new HashMap<>();
Cat cat = new Cat("molly");
cats.put(cat, "This is a cool cat");
System.out.println(cats.get(new Cat("molly"));
如果您没有实现hashCode(),则在执行前面的代码后将打印null。
其他回答
简单地说,==检查两个对象是否指向相同的内存位置,而.equals()计算对象中值的比较。
根据你谈论的是“原语”还是“对象类型”,有一些小的区别;如果你在谈论“静态”或“非静态”成员,也可以这样说;你也可以混合上面所有的…
下面是一个例子(你可以运行它):
public final class MyEqualityTest
{
public static void main( String args[] )
{
String s1 = new String( "Test" );
String s2 = new String( "Test" );
System.out.println( "\n1 - PRIMITIVES ");
System.out.println( s1 == s2 ); // false
System.out.println( s1.equals( s2 )); // true
A a1 = new A();
A a2 = new A();
System.out.println( "\n2 - OBJECT TYPES / STATIC VARIABLE" );
System.out.println( a1 == a2 ); // false
System.out.println( a1.s == a2.s ); // true
System.out.println( a1.s.equals( a2.s ) ); // true
B b1 = new B();
B b2 = new B();
System.out.println( "\n3 - OBJECT TYPES / NON-STATIC VARIABLE" );
System.out.println( b1 == b2 ); // false
System.out.println( b1.getS() == b2.getS() ); // false
System.out.println( b1.getS().equals( b2.getS() ) ); // true
}
}
final class A
{
// static
public static String s;
A()
{
this.s = new String( "aTest" );
}
}
final class B
{
private String s;
B()
{
this.s = new String( "aTest" );
}
public String getS()
{
return s;
}
}
您可以比较“==”(相等运算符)和“.equals(…)”的解释。(java.lang.Object类中的方法)通过以下链接:
= = http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html .equals(…):http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html # = (java . lang . object)
==是一个操作符,equals()是一个方法。
运算符通常用于基元类型比较,因此==用于内存地址比较,equals()方法用于对象比较。
Equals()方法主要比较对象的原始内容。
如果我们写作
String s1 = "Samim";
String s2 = "Samim";
String s3 = new String("Samim");
String s4 = new String("Samim");
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
System.out.println(s3.equals(s4));
输出将是
true
true
true
因为equals()方法比较对象的内容。 在first System.out.println()中,s1和s2的内容是相同的,这就是为什么它打印为真。 另外两个System.out.println()为真。
再一次,
String s1 = "Samim";
String s2 = "Samim";
String s3 = new String("Samim");
String s4 = new String("Samim");
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s3 == s4);
输出将是
true
false
false
因为==运算符主要比较对象的引用,而不是对象的值。 在first System.out.println()中,s1和s2的引用是相同的,这就是为什么它返回true。
在第二个System.out.println()中,创建了s3对象,这就是为什么会创建另一个s3的引用,并且s2和s3的引用会有所不同,因此它返回“false”。
Third System.out.println(),遵循second System.out.println()的规则,这就是为什么它会返回"false"。
对于String类:
equals()方法比较String实例中的“值”(在堆上),而不考虑两个对象引用是否引用同一个String实例。如果任意两个String类型的对象引用引用同一个String实例,那就太好了!如果两个对象引用引用两个不同的String实例..这没什么区别。它是每个被比较的String实例中的“值”(即:字符数组的内容)。
On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".
就像任何解释一样,让它深入人心。
我希望这能让你清楚一点。