我想澄清一下我的理解是否正确:

==是一个引用比较,即两个对象都指向相同的内存位置 .equals()计算为对象中值的比较


当前回答

对于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".

就像任何解释一样,让它深入人心。

我希望这能让你清楚一点。

其他回答

要在自定义类中使用equals函数,您必须重写equals函数(以及其他函数)。

equals方法比较对象。

==二进制操作符比较内存地址。

如果不重写.equals(), ==和.equals()都指向同一个对象。

一旦你重写了.equals(),你想要做的就是你的愿望。你可以比较调用对象的状态和传入对象的状态或者你可以调用super。equals()

只需记住.equals(…)必须由您试图比较的类实现。否则,就没有什么意义了;Object类的方法版本所做的事情与比较操作相同:

唯一需要对对象使用比较运算符的时候是比较枚举的时候。这是因为一次只有一个Enum值的实例。例如,给定枚举

enum FooEnum {A, B, C}

你永远不会同时拥有一个以上的A实例,对于B和c也是如此。这意味着你实际上可以像这样编写一个方法:

public boolean compareFoos(FooEnum x, FooEnum y)
{
    return (x == y);
}

你就不会有任何问题了。

由于Java不支持操作符重载,==的行为是相同的 除equals()外的每个对象都可以使用is方法,该方法可以在 可以根据业务更改用于比较对象的Java和逻辑 规则。

在Java中==和=的主要区别是“==”被用来 建议检查equals()方法时比较原语 对象相等。

字符串比较是同时使用==和equals()方法的常见场景。由于java.lang.String类重写等于方法,它 如果两个String对象包含相同的内容,但== will返回true 只有当两个引用指向同一个对象时才返回true。

下面是一个使用==和equals()方法比较Java中两个string是否相等的例子,这将消除一些疑问:

 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);

    }
    }

对于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".

就像任何解释一样,让它深入人心。

我希望这能让你清楚一点。