equals and = = in Java

Source: Internet
Author: User

Equals () and = = are all used for comparison in Java and return a Boolean value, unlike Equals () is a method defined in the object class, = =

is a comparison operator. Here is the source code for equals () in object:

public boolean equals (Object obj) {return (this = = obj); }

We are surprised to find that this method and = = is not the same?? Let's look at the source code for the Equals () method in the String class:

Public boolean equals (Object anobject)  {        // First, the value of the address is judged, if you want to return directly to true        if  (This == anobject)  {            return true;         }        //if the addresses are not equal, Determine if the object passed in is not an instance of String type         if  (anobject instanceof  string)  {            //Discovery is an instance of String type, Turn it into a string type (a way to call string after a strong turn)              string anotherstring =  (String)  anObject;             //value is a char array inside a string, with each string object corresponding to a char array, to get the length              int n = value.length;            // Compare the lengths first, and if they are the same, proceed to the next             if  (n ==  anotherstring.value.length)  {                 //finds the same length, compares the characters in a char array of string to one by one                  char v1[] = value;                 char v2[] =  anotherstring.value;                 int i = 0;                 while  (n-- != 0)  {                     if  (V1[i] != v2[i])                               return false;                     i++;                 }                 return true;             }        }         return false;    }

It is obvious that string overrides the Equals () method so that it has the ability to compare the equality of string content, when equals () is not merely comparing object addresses.

If the address is not the same, he compares the object content to the same, and returns True if the same.

Summarized as follows:

Equals () and = = are used for comparison operations.

Equals () is a method in the object class that is used to compare whether an object is equal and what it means in the object class.

Same as = =, compared to the address of the object, but sometimes we want to compare the object's contents, at this time to override the Equals method, for example, the string class overrides the Equals method by default

Compares the contents of an object.

= = is a comparison operator that, when both sides are basic data types, directly compares the values of two data, and when both sides are objects, the address of the object is compared.

equals and = = in Java

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.