65

我今天换了讲师,他用一个奇怪的代码对我说。(他说最好用.equals,当我问为什么时,他回答“因为它是!”)

所以这里有一个例子:

if (o1.equals(o2))
{
 System.out.println("Both integer objects are the same");
}

而不是我习惯的:

if (o1 == o2)
{
  System.out.println("Both integer objects are the same");
}

两者有什么区别。为什么他的方式(使用.equals)更好?

在快速搜索中找到了这个,但我无法理解这个答案:

4

11 回答 11

112

在Java 中,==总是只比较两个引用(即对于非原始对象)——即它测试两个操作数是否引用同一个对象。

但是,该equals方法可以被覆盖——因此两个不同的对象仍然可以相等。

例如:

String x = "hello";
String y = new String(new char[] { 'h', 'e', 'l', 'l', 'o' });

System.out.println(x == y); // false
System.out.println(x.equals(y)); // true

此外,值得注意的是,任何两个相等的字符串常量(主要是字符串文字,但也包括通过串联的字符串常量组合)最终将引用同一个字符串。例如:

String x = "hello";
String y = "he" + "llo";
System.out.println(x == y); // true!

这里xy是对同一个字符串的引用,因为y是一个编译时常量,等于"hello".

于 2009-10-29T11:29:57.353 回答
20

== 运算符比较对象是否是相同的实例。equals() 操作符比较对象的状态(例如,如果所有属性都相等)。当一个对象等于另一个对象时,您甚至可以重写 equals() 方法来定义自己。

于 2009-10-29T11:30:50.827 回答
14

如果你我每个人走进银行,每个人都开一个全新的账户,每个人都存入 100 美元,那么……

  1. myAccount.equals(yourAccount)true因为它们具有相同的值,但是
  2. myAccount == yourAccountfalse因为他们不是同一个账号

(当然,假设类的适当定义Account。;-)

于 2009-10-29T11:37:18.777 回答
2

== 是一个运算符。equals 是 Object 类中定义的方法

== 检查两个对象在内存中是否具有相同的地址,对于原语,它检查它们是否具有相同的值。equals 方法另一方面检查正在比较的两个对象是否具有相等的值(取决于当然已经为对象实现了 equals 方法。equals 方法不能应用于基元(这意味着如果 a 是基元 a.equals(someobject) 是不允许的,但是 someobject.equals(a) 是允许的)。

于 2012-06-08T16:26:03.080 回答
0

== operator compares two object references to check whether they refer to same instance. This also, will return true on successful match.for example

public class Example{
public static void main(String[] args){
String s1 = "Java";
String s2 = "Java";
String s3 = new string ("Java");
test(Sl == s2)     //true
test(s1 == s3)      //false
}}

above example == is a reference comparison i.e. both objects point to the same memory location

String equals() is evaluates to the comparison of values in the objects.

   public class EqualsExample1{
   public static void main(String args[]){
   String s = "Hell";
   String s1 =new string( "Hello");
   String s2 =new string( "Hello");
   s1.equals(s2);    //true
    s.equals(s1) ;   //false
    }}

above example It compares the content of the strings. It will return true if string matches, else returns false.

于 2015-09-18T05:10:41.720 回答
-1
于 2014-05-08T12:58:04.970 回答
-1
public static void main(String[] args){
        String s1 = new String("hello");
        String s2 = new String("hello");

        System.out.println(s1.equals(s2));
        ////
        System.out.println(s1 == s2);

    System.out.println("-----------------------------");

        String s3 = "hello";
        String s4 = "hello";

        System.out.println(s3.equals(s4));
        ////
        System.out.println(s3 == s4);
    }

Here in this code u can campare the both '==' and '.equals'

here .equals is used to compare the reference objects and '==' is used to compare state of objects..

于 2015-03-20T13:19:56.120 回答
-2
于 2013-07-01T19:24:48.590 回答
-2

Lets say that "==" operator returns true if both both operands belong to same object but when it will return true as we can't assign a single object multiple values

public static void main(String [] args){
    String s1 = "Hello";
    String s1 = "Hello";  // This is not possible to assign multiple values to single object
    if(s1 == s1){
      // Now this retruns true
   }
}

Now when this happens practically speaking, If its not happen then why this is == compares functionality....

于 2014-09-26T14:20:02.123 回答
-2

(1) == can be be applied for both primitives and object types, but equals() method can be applied for only object types.

(2) == cannot be overridden for content comparison, but equals method can be overridden for content comparison(ex; String class, wrapper classes, collection classes).

(3) == gives incomparable types error when try to apply for heterogeneous types , where as equals method returns false.

于 2015-08-21T03:37:14.293 回答
-2

Here is a simple interpretation about your problem:

== (equal to) used to evaluate arithmetic expression

where as

equals() method used to compare string

Therefore, it its better to use == for numeric operations & equals() method for String related operations. So, for comparison of objects the equals() method would be right choice.

于 2016-02-14T03:30:42.110 回答