= = and equals () analysis and source code analysis in Java

Source: Internet
Author: User

1. About = =

The first thing to know is that = = is used to match the contents of the memory unit, is actually a number, the computer is only a number, and in the Java language, when the = = match, is more than two units of memory content is the same.

If they are primitive types, byte,boolean,short,char,int,long,float,double is the direct comparison of their values.

If it is a reference, the reference value can be considered to be the logical address of the object, if two references occur = = operation, is to compare the address value of two corresponding objects is the same, in other words, if two references hold the same object, return True, otherwise false.

2. About Equals ()

The Equals method, first defined in object, is defined by using the = = method, that is, if the Equals method is not overridden and the corresponding class has not overridden the Equals method in its parent class list, Then the default equals action is to compare the address of the object.

The Equals method exists, it is hoped that the subclass to rewrite this method, to achieve the function of the contrast value, similar to the string to implement the method itself.

equals of JDK1.7 ()

public boolean equals (Object anobject) {

if (this==anobject) {

return true;

}

if (anobject instanceof String) {

String anotherstring = (string) anobject;

int n=value.length;

if (n==anotherstring.value.length) {

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;

}

Note: The code logic for either 1.6 or 1.7,string.equals () is roughly the following:

(1) Determines whether the incoming object and the current object are the same object, and if so, returns true directly.

(2) Determines whether the passed-in type value is a string, or false if not;

(3) Determine whether the incoming string and the current string of the bed are consistent, if inconsistent returns false;

(4) The loop compares the two strings of the char[] array, one by one, whether the characters are consistent, if there is inconsistency, the direct return false;

(5) No mismatch was found at the end of the loop, so the last return is true;

= = and equals () analysis and source code analysis 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.