Java中==和equals()之间的区别是什么?
我想澄清我是否正确理解了这一点:Java中==和equals()之间的区别是什么?,java,identity,equality,object-comparison,Java,Identity,Equality,Object Comparison,我想澄清我是否正确理解了这一点: =是一个参考比较,即两个对象指向同一个内存位置 .equals()计算对象中值的比较 一般来说,你的问题的答案是“是”,但是 .equals(…)将只比较编写来比较的内容,不多也不少 如果一个类没有重写equals方法,那么它默认为重写此方法的最近父类的equals(Object o)方法 如果没有父类提供覆盖,那么它默认为来自最终父类Object的方法,因此剩下Object#equals(objecto)方法。根据对象API,这与==相同;也就是说,当且仅
是一个参考比较,即两个对象指向同一个内存位置=
计算对象中值的比较.equals()
将只比较编写来比较的内容,不多也不少.equals(…)
- 如果一个类没有重写equals方法,那么它默认为重写此方法的最近父类的
方法李>equals(Object o)
- 如果没有父类提供覆盖,那么它默认为来自最终父类Object的方法,因此剩下
方法。根据对象API,这与Object#equals(objecto)
相同;也就是说,当且仅当两个变量引用同一个对象时,如果它们的引用是一个且相同的,则返回true。因此,您将测试对象相等性,而不是功能相等性==
- 如果您覆盖了
,请务必记住覆盖equals
,以免“违反合同”。根据API,如果两个对象的hashCode
方法显示它们是等效的,则从equals
方法返回的结果必须是相同的。反之不一定正确李>hashCode()
一旦重写.equals(),你想做的就是你的愿望。您可以将调用对象的状态与传入对象的状态进行比较,也可以只调用super.equals()请记住,
.equals(…)
必须由您尝试比较的类实现。否则,就没有什么意义了;对象类的方法版本执行与比较操作相同的操作:
只有在比较枚举时,才真正希望对对象使用比较运算符。这是因为一次只有一个枚举值实例。例如,给定枚举
enum FooEnum {A, B, C}
您一次将不会有一个以上的A
实例,而B
和C
的实例也是如此。这意味着您可以编写如下方法:
public boolean compareFoos(FooEnum x, FooEnum y)
{
return (x == y);
}
您将不会遇到任何问题。关于字符串类: equals()方法比较字符串实例(堆上)内的“值”,而不管两个对象引用是否引用同一个字符串实例。如果任何两个String类型的对象引用引用同一个String实例,那就太好了!如果两个对象引用引用两个不同的字符串实例。。这没什么区别。它是被比较的每个字符串实例中的“值”(即:字符数组的内容) 另一方面,“==”操作符比较两个对象引用的值,以查看它们是否引用相同的字符串实例。如果两个对象引用的值“引用”同一个字符串实例,那么布尔表达式的结果将是“true”。.duh。另一方面,如果两个对象引用的值“引用”不同的字符串实例(即使两个字符串实例具有相同的“值”,即每个字符串实例的字符数组的内容相同),则布尔表达式的结果将为“false” 就像任何解释一样,让它沉入其中 我希望这能把事情弄清楚一点。
=
是一个运算符,equals()
是一个方法
运算符通常用于基本类型比较,因此
==
用于内存地址比较,equals()
方法用于比较对象 根据您所谈论的是“原语”还是“对象类型”,有一些细微的差别;如果你说的是“静态”或“非静态”成员,也可以这样说;你也可以混合以上所有的
下面是一个示例(您可以运行它):
您可以通过以下链接比较“==”(相等运算符)和“.equals(…”(java.lang.Object类中的方法)的解释:
- ==:
- .等于(…):
Long a = 10L;
Long b = 10L;
if (a == b) {
System.out.println("Wrapped primitives behave like values");
}
相比之下,将上述两个long放入两个单独的ArrayList中,equals将它们视为相同的,但==不会
ArrayList<Long> c = new ArrayList<>();
ArrayList<Long> d = new ArrayList<>();
c.add(a);
d.add(b);
if (c == d) System.out.println("No way!");
if (c.equals(d)) System.out.println("Yes, this is true.");
ArrayList c=new ArrayList();
ArrayList d=新的ArrayList();
c、 添加(a);
d、 添加(b);
如果(c==d)System.out.println(“不行!”);
如果(c等于(d))System.out.println(“是的,这是真的”);
=
可以在许多对象类型中使用,但您可以对任何类型使用object.equals
,尤其是字符串和谷歌地图标记。由于Java不支持运算符重载,=
的行为相同
对于每个对象,但equals()
是方法,可以在
可以根据业务更改比较对象的Java和逻辑
规则
Java中==
和equals之间的主要区别在于“==”
用于
比较原语,同时建议使用equals()
方法进行检查
对象的相等性
字符串比较是同时使用=
和equals()
方法的常见场景。由于java.lang.String类重写了equals方法,因此
如果t,则返回true
ArrayList<Long> c = new ArrayList<>();
ArrayList<Long> d = new ArrayList<>();
c.add(a);
d.add(b);
if (c == d) System.out.println("No way!");
if (c.equals(d)) System.out.println("Yes, this is true.");
public class TEstT{
public static void main(String[] args) {
String text1 = new String("apple");
String text2 = new String("apple");
//since two strings are different object result should be false
boolean result = text1 == text2;
System.out.println("Comparing two strings with == operator: " + result);
//since strings contains same content , equals() should return true
result = text1.equals(text2);
System.out.println("Comparing two Strings with same content using equals method: " + result);
text2 = text1;
//since both text2 and text1d reference variable are pointing to same object
//"==" should return true
result = (text1 == text2);
System.out.println("Comparing two reference pointing to same String with == operator: " + result);
}
}
class A
{
int id;
String str;
public A(int id,String str)
{
this.id=id;
this.str=str;
}
public static void main(String arg[])
{
A obj=new A(101,"sam");
A obj1=new A(101,"sam");
obj.equals(obj1)//fasle
obj==obj1 // fasle
}
}
class A
{
int id;
String str;
public A(int id,String str)
{
this.id=id;
this.str=str;
}
public boolean equals(Object obj)
{
A a1=(A)obj;
return this.id==a1.id;
}
public static void main(String arg[])
{
A obj=new A(101,"sam");
A obj1=new A(101,"sam");
obj.equals(obj1)//true
obj==obj1 // fasle
}
}
assert "ab" == "a" + "b";
Integer i = 1;
Integer j = i;
assert i == j;
assert new String("a") != new String("a");
Integer i = 128;
Integer j = 128;
assert i != j;
assert (new String("a")).equals(new String("a"));
Integer i = 128;
Integer j = 128;
assert i.equals(j);
String mango = "mango";
String mango2 = "mango";
System.out.println(mango != mango2);
System.out.println(mango == mango2);
false
true
true
false
String mango = "mango";
String mango3 = new String("mango");
System.out.println(mango != mango3);
System.out.println(mango == mango3);
false
true
true
false
String mango = "mango";
String mango2 = "mango";
String mango3 = new String("mango");
System.out.println(mango != mango2);
System.out.println(mango == mango2);
System.out.println(mango3 != mango2);
System.out.println(mango3 == mango2);
// mango2 = "mang";
System.out.println(mango+" "+ mango2);
System.out.println(mango != mango2);
System.out.println(mango == mango2);
System.out.println(System.identityHashCode(mango));
System.out.println(System.identityHashCode(mango2));
System.out.println(System.identityHashCode(mango3));
false
true
true
false
mango mango
false
true
17225372
17225372
5433634
mango = "mango";
mango ----> "mango"
mango2 = "mango";
mango ----> "mango" <---- mango2
mango3 = new String("mango")
mango -----> "mango" <------ mango2
mango3 ------> "mango"
mango ---->"mango"
mango2 ----> "mang"
mango3 -----> "mango"
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"));
String foo = new String("abc");
String bar = new String("abc");
if(foo==bar)
// False (The objects are not the same)
bar = foo;
if(foo==bar)
// True (Now the objects are the same)
String foo = new String("abc");
String bar = new String("abc");
if(foo.equals(bar))
// True (The objects are identical but not same)
public class StringPool {
public static void main(String[] args) {
String s1 = "Cat";// will create reference in string pool of heap memory
String s2 = "Cat";
String s3 = new String("Cat");//will create a object in heap memory
// Using == will give us true because same reference in string pool
if (s1 == s2) {
System.out.println("true");
} else {
System.out.println("false");
}
// Using == with reference and Object will give us False
if (s1 == s3) {
System.out.println("true");
} else {
System.out.println("false");
}
// Using .equals method which refers to value
if (s1.equals(s3)) {
System.out.println("true");
} else {
System.out.println("False");
}
}
}
String w1 ="Sarat";
String w2 ="Sarat";
String w3 = new String("Sarat");
System.out.println(w1.hashCode()); //3254818
System.out.println(w2.hashCode()); //3254818
System.out.println(w3.hashCode()); //3254818
System.out.println(System.identityHashCode(w1)); //prints 705927765
System.out.println(System.identityHashCode(w2)); //prints 705927765
System.out.println(System.identityHashCode(w3)); //prints 366712642
if(w1==w2) // (705927765==705927765)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
//prints true
if(w2==w3) // (705927765==366712642)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
//prints false
if(w2.equals(w3)) // (Content of 705927765== Content of 366712642)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
//prints true
String s1 = new String("Ali");
String s2 = new String("Veli");
String s3 = new String("Ali");
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
System.out.println("(s1==s2):" + (s1 == s2));
System.out.println("(s1==s3):" + (s1 == s3));
System.out.println("s1.equals(s2):" + (s1.equals(s2)));
System.out.println("s1.equal(s3):" + (s1.equals(s3)));
/*Output
96670
3615852
96670
(s1==s2):false
(s1==s3):false
s1.equals(s2):false
s1.equal(s3):true
*/
String string1 = "Ravi";
String string2 = "Ravi";
String string3 = new String("Ravi");
String string4 = new String("Prakash");
System.out.println(string1 == string2); // true because same reference in string pool
System.out.println(string1 == string3); // false
System.out.println(string1.equals(string2)); // true equals() comparison of values in the objects
System.out.println(string1.equals(string3)); // true
System.out.println(string1.equals(string4)); // false
public class Conditionals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println("is str1 == str2 ? " + (str1 == str2 ));
System.out.println("is str1.equals(str2) ? " + (str1.equals(str2 )));
}
}
The result is
is str1 == str2 ? false
is str1.equals(str2) ? true
public class Conditionals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
System.out.println("is str1 == str2 ? " + (str1 == str2 ));
System.out.println("is str1.equals(str2) ? " + (str1.equals(str2 )));
}
}
The result is
is str1 == str2 ? true
is str1.equals(str2) ? true
package com.tadtab.CS5044;
public class Person {
private double height;
private double weight;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
/**
* This method uses the height as a means of comparing person objects.
* NOTE: weight is not part of the comparison criteria
*/
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
return true;
}
public static void main(String[] args) {
Person person1 = new Person();
person1.setHeight(5.50);
person1.setWeight(140.00);
Person person2 = new Person();
person2.setHeight(5.70);
person2.setWeight(160.00);
Person person3 = new Person();
person3 = person2;
Person person4 = new Person();
person4.setHeight(5.70);
Person person5 = new Person();
person5.setWeight(160.00);
System.out.println("is person1 == person2 ? " + (person1 == person2)); // false;
System.out.println("is person2 == person3 ? " + (person2 == person3)); // true
//this is because perosn3 and person to refer to the one person object in memory. They are aliases;
System.out.println("is person2.equals(person3) ? " + (person2.equals(person3))); // true;
System.out.println("is person2.equals(person4) ? " + (person2.equals(person4))); // true;
// even if the person2 and person5 have the same weight, they are not equal.
// it is because their height is different
System.out.println("is person2.equals(person4) ? " + (person2.equals(person5))); // false;
}
}
is person1 == person2 ? false
is person2 == person3 ? true
is person2.equals(person3) ? true
is person2.equals(person4) ? true
is person2.equals(person4) ? false