Java ==,equals () and Hashcode

Source: Internet
Author: User

Kruger class said = = and equals () method is different, after the query will be specific content, in the query process found Hashcode () method and Equlas () contact closely, so study together.

More superficial, later if understanding more at any time update.

The following section of code mainly explains the difference between = = and equals

String a = "Nihao";

String B = "Nihao";

String d = "Niha";

String e = new String ("Nihao");

String f = new string ("Nihao");

System.out.println (a==b);//true

System.out.println (A==d);//false

System.out.println (a==e);//false

System.out.println (A.equals (e));//true

System.out.println (e==e);//true

System.out.println (e==f);//false

System.out.println (E.equals (f))//true

1. = =

= = is a reference comparsion. Both objects point to the same memory location.

and = = used for basic types, such as int,float, compare values.

For object types, = = compares the addresses of two objects at the JVM. For example A and B, both are references to the string "Nihao", pointing to the same address. A and D refer to different strings, with different addresses.

New object addresses exist on the heap, so e is different from a address. In the same vein, e==f is also wrong.

2. Equals ()

equals would only compare what it was writtern to compare. If A class does not override the Equals methods, it defaults to the Equals (object O) Methods (inherited from the nearest parent class). If none is override, then equals inherits from the root class object. The default method is also to compare object addresses.

But some subclasses have already override this method, such as String. In this class, equals first compares the JVM address of the object, and if it is a reference to the same object (E==e), the object is equal and returns true. If it is not an object (such as A and e), equals will compare the characters within the string object, and return true exactly the same. Specifically, you can query the source code of the Equals method.

This can explain why it is necessary to use equals for string types.

And so on double, inteter, Math, all are the override Equals (), the comparison is the content.

It is important to note that equals is the class that is being carried out and the comparison of the values in the class, do not directly compare two values with equals, for example:

Double A = 1;

Double b = 1;

System.out.prinln (A.equals (b)); It is clear that this statement is wrong.

The Java language has the following requirements for Equals ():
A: Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true".
B: reflectivity: X.equals (x) must return is "true".
C: analogical: If X.equals (y) returns "true" and Y.equals (z) returns "true", then Z.equals (x) should also return "true".
D: Consistency: if X.equals (y) returns is "true", as long as the X and Y contents remain constant, no matter how many times you repeat X.equals (y), the return is "true".
E: In any case, x.equals (NULL) will always return "false"; X.equals (and X objects of different types) will always return "false".


3. Hashcode ()

Always remember to override hashcode if your override equals so as not to ' break the constract '. As per the API, the result returned from the hashcode () Mehod for both objects must be the same if their Equals methods sho WS that they ar equivalent. The converse is not necessairly.

Hashcode is also a method in the root class object, which by default returns 32 of the object as the JVM memory address.

Here is an explanation of the above paragraph in English:

The general contract for the Constract:hashcode method, which indicates that the equality object must have an equal hash code. (What is a hash code?) Because the hash code retrieves the object through Hashcode.

If you override the Equals method, be sure to override the Hashcode method, and if you do not override, the Hashcode method in object objects always returns the hash address of an object, and this address is never equal, so even if you override equals , and there is no specific effect. The Equals method is not called because the hashcode is not equal

Java ==,equals () and Hashcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.