== 相等性检查运算符的工作功能是什么?它在内部使用哈希码方法吗?

huangapple go评论75阅读模式
英文:

== equality check operator working functionality do it use hashcode method internally?

问题

// 程序片段

String fullName = "AlexAeva";
System.out.println("fullName hc " +fullName.hashCode());

String firstName = "Alex";
System.out.println("firstName hc " + firstName.hashCode());

String lastName = "Aeva";
System.out.println("lastName hc " + lastName.hashCode());

firstName += lastName;

System.out.println("firstName hc " + firstName.hashCode());

// 名字和全名的哈希码相同
// 但是等号操作符返回false。
// 在内部使用 == 使用哈希码进行识别

System.out.println(" == check " + (firstName == fullName));
英文:
    // Program snippet
 
    String fullName = "AlexAeva";
    System.out.println("fullName hc " +fullName.hashCode());
    
    String firstName = "Alex";
    System.out.println("firstName hc " + firstName.hashCode());
    
    String lastName = "Aeva";
    System.out.println("lastName hc " + lastName.hashCode());
    
    firstName += lastName;
    
    System.out.println("firstName hc " + firstName.hashCode());
    
    // hash code of first name and fullname is same 
    // But equality operator is giving false.
    // DO == use hashcode for  identification internally
    
    
    System.out.println(" == check " + (firstName == fullName));

Result :-

>fullName hc 1684075821
firstName hc 2043454
lastName hc 2037231
firstName hc 1684075821
== check false

答案1

得分: 1

非常好的观察。

然而,理解哈希码(hashcode)和.equals()之间的区别很重要。

哈希码只是对象的哈希值。它对于定义相等性(或者更确切地说,证明不等性)很有用。

.equals()告诉你ObjectA是否被认为等于ObjectB,基于对象对相等性的定义。相等性可能由哈希码定义的一个方面构成。

== 告诉你ObjectA是否指向并引用了与ObjectB相同的数据。基本上,它们是否是同一个对象。

为了给出更好的例子,让我们假设你和我都有一个装有10个三明治的盒子。哈希码就好像我们各自把盒子放在独立的秤上,看它们是否重量相同。.equals()则是打开盒子,看里面是否有相同类型的三明治(在某种程度的任意细节上)。== 则是我们查看是否持有同一个盒子。

考虑这个:

public class SOQ_20200913_2 {

   public SOQ_20200913_2() {
   
   // 程序片段
   
      String fullName = "AlexAeva";
      System.out.println("fullName 的哈希码 " + fullName.hashCode());
      
      String firstName = "Alex";
      System.out.println("firstName 的哈希码 " + firstName.hashCode());
      
      String lastName = "Aeva";
      System.out.println("lastName 的哈希码 " + lastName.hashCode());
      
      firstName += lastName;
      
      System.out.println("firstName 的哈希码 " + firstName.hashCode());
      
      // firstName 和 fullName 的哈希码相同
      // 但相等运算符给出了 false。
      // == 内部是否使用哈希码进行识别?
   
      System.out.println("哈希码检查 " + (firstName.hashCode() == fullName.hashCode()));
      System.out.println(".equals 检查 " + (firstName.equals(fullName)));
      System.out.println("== 检查 " + (firstName == fullName));
   
   }

   public static void main(String[] args) {
   
      SOQ_20200913_2 soq = new SOQ_20200913_2();
   
   }

}
英文:

Very good observation.

However, it is important to understand the difference between hashcode and .equals.

Hashcode is just the hash of an object. It is useful for defining equality (or rather, proving inequality).

.equals() tells you whether or not ObjectA is considered equal to ObjectB, BASED ON THE OBJECT'S DEFINITION OF equality. One aspect equality might be defined by is the hashcode.

== tells you whether or not ObjectA is literally pointing to and referencing the same data that ObjectB is. Basically, are they the same object.

To give a better example, let's assume that both you and I have a box of 10 sandwiches. A Hashcode would be both of us putting our bags on individual scales and seeing if they weigh the same. .equals() would be opening up the bags to see if they have the same type of sandwiches (down to some arbitrary level of detail). == would be us seeing if we are both holding the same bag.

Consider this:

public class SOQ_20200913_2
{

   public SOQ_20200913_2()
   {
   
   // Program snippet
   
      String fullName = "AlexAeva";
      System.out.println("fullName hc " +fullName.hashCode());
      
      String firstName = "Alex";
      System.out.println("firstName hc " + firstName.hashCode());
      
      String lastName = "Aeva";
      System.out.println("lastName hc " + lastName.hashCode());
      
      firstName += lastName;
      
      System.out.println("firstName hc " + firstName.hashCode());
      
      // hash code of first name and fullname is same 
      // But equality operator is giving false.
      // DO == use hashcode for  identification internally
   
      System.out.println(" hash check " + (firstName.hashCode() == fullName.hashCode()));
      System.out.println(".equals check " + (firstName.equals(fullName)));
      System.out.println(" == check " + (firstName == fullName));
   
   }

   public static void main(String[] args)
   {
   
      SOQ_20200913_2 soq = new SOQ_20200913_2();
   
   }

}

答案2

得分: 0

No. ==比较对象的引用相等性。也就是说,当且仅当两个对象真正相同时,它们是相等的。如果您想按哈希码进行比较,您需要显式调用hashCode。如果您希望进行合理且与hashCode一致的等式比较,可以使用equals方法。

英文:

No. == compares objects to reference equality. That is, two objects are equal if and only if they're literally the same. If you want to compare by hash code, you need to call hashCode explicitly. If you want an equality comparison that's at least somewhat reasonable and agrees with hashCode, you can use the equals method.

答案3

得分: 0

==, hashcode(), equals()

equals() 和 == 用于比较两个对象。以下是它们之间的一些区别:
== 运算符用于地址比较,而 .equals() 方法用于内容比较。

equals 方法用于检查两个对象是否包含相同的数据。

示例代码:

public static void main(String[] args) {
    String s1 = new String("Kamal");
    String s2 = new String("Kamal");
    String s3 = new String("Pasindu");

    System.out.println(s1 == s2);      //false
    System.out.println(s1.equals(s2)); //true
    System.out.println(s1.equals(s3)); //false
}

在 hashcode() 方法中,它返回对象的哈希码值。

  1. 如果两个对象根据 equals() 方法是相等的,则它们的哈希码必须相同。
public static void main(String[] args) {
    String s1 = new String("Kamal");
    String s2 = new String("Kamal");
    String s3 = new String("Pasindu");

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

    System.out.println(s1.hashCode() == s2.hashCode()); //true
    System.out.println(s1.hashCode() == s3.hashCode()); //false
}
英文:

==, hashcode(), equals()

equals() and == used to compare 2 Objects.here are some of the differences between the two.
== operators for address comparison and .equals() method for content comparison.

equals method to check whether two objects contains the same data or not.

Ex code:

public static void main(String[] args) {
        String s1=new String("Kamal");
        String s2=new String("Kamal");
        String s3=new String("Pasindu");
        
        System.out.println(s1==s2); //false
        System.out.println(s1.equals(s2)); //true
        System.out.println(s1.equals(s3)); //false
    }

In hashcode() method it is return the hashcode value of the Object.

  1. If two objects are equal according to equals() method, then their hash code must be same.

public static void main(String[] args) {

        String s1 = new String("Kamal");
        String s2 = new String("Kamal");
        String s3 = new String("Pasindu");

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

        System.out.println(s1.hashCode() == s2.hashCode()); //true
        System.out.println(s1.hashCode() == s3.hashCode());  //false
    }

huangapple
  • 本文由 发表于 2020年9月14日 11:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定