in Java = = and Equals () detailed

Source: Internet
Author: User

Data types in Java are divided into two categories:

First, the basic data type:

BYTE, short, int, long, float, double, char, Boolean

Compare them with = =, compare their values to be equal

Second, the reference data type:

That is, the basic data type of encapsulation, with = = comparison is their memory address (in fact, the basic data type of comparison, their memory address is not an int). When new, it will give it a new memory address, so again by = = comparison, it will return false, the Equals method in the object class is actually the memory address, with the = = and the Equals method to compare the result is the same, but in some classes to rewrite the Equals method, such as String, Integer, and so on, instead of simply comparing memory addresses.

  This equals method is not fixed, and when necessary, we rewrite it according to the situation.

Here is a simple code to test it, inside the note to pay attention to see Oh! Where the Java source code at the end of the article has

public class text{public static void Main (string[] args) {String str = "ABCD";        String str1 = "ABCD1";        int i = 1;        String str2 = new String ("ABCD");                What is the memory address, and what is the memory address? A 16-binary shaped Value//java object memory address a hash algorithm to derive an int type number System.out.println ("abcd1" = = str1);        Using the Equals method of string, the string is compared System.out.println (Str.equals ("ABCD"));        In Javac, the virtual machine automatically combines the strings???        System.out.println (Str.equals (' a ' + "BCD")); Compared to the memory address, STR2 created, using a keyword new, explicitly tell the virtual machine, I want a new, so the string "ABCD" is treated as a new creation, and there is a new memory address if (str = = str2) {Syst        Em.out.println ("str = = str2");            }else{System.out.println ("str! = str2");            The Hashcode method called in the string (the end of the article has Java source code), so equal. What is the Hashcode method in string? Is it ASCII?            It is indeed ASCII, the author has counted it,/////somewhere we need to rewrite the Hashcode method, where? The most common thing is that when an object is added to the collection objects (collection Object), the two functions must be designed themselves. The more granular definition is: If you want to put an object A in another collection object B,
Or use this object A to find a Meta object in the collection object B in the location of the key, and to support whether to accommodate, delete the object B in the collection of meta-objects such operations, then, the Equals () and hashcode () function must be defined by the developer.
In other cases, these two functions are not required to be defined. System.out.println (Str.hashcode ()); System.out.println (Str2.hashcode ()); }//Call string of equals java.lang.String in 964 lines equals method//String bottom is a character array, so the Equals method in string uses two char array, the opposite character one by one comparison The object content comparison is the true purpose of design equals (), and the Java language requirements for equals () are required to be followed. Otherwise, you shouldn't waste time://symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true". Reflectivity: X.equals (x) must return is "true". Analogy: If X.equals (y) returns "true" and Y.equals (z) returns "true", then Z.equals (x) should also return "true". Consistency: if X.equals (y) returns "true", the return is "true" as long as the X and Y contents remain constant, no matter how many times you repeat X.equals (y). In any case, x.equals (NULL) will always return "false", and X.equals (and X objects of different types) will always return "false". In some cases, we need to rewrite the Equals () method ourselves, depending on the situation if (Str.equals (str2)) {System.out.println ("str equals str2 " ); }else{System.out.println ("str is not equals str2"); } }}

Operation Result:

Summarize:

  = =: Compare the basic data types, compare their values

  equals (): The reference data type is compared, the reference data type is compared, the different types have different equals methods, and different equals methods are called according to different data types. You can also override the Equals method in special cases.

Equals () in object:

Equals () in string:

Hashcode () in string:

in Java = = and Equals () detailed

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.