Java Equals method detailed

Source: Internet
Author: User

In Java, the Equals method is defined in the object class, and the Equals method in the object class is the source code, as you can see from the source code that the Equals method in the object class is used to return to determine whether two objects point to the same address

public boolean equals (Object obj) {

return (this = = obj);

}

The Equals method in the Ojbect class overrides the quilt class, for example, string overrides the Equals method, and the Equals method of string has the following source code,

public boolean equals (Object anobject) {

Determines whether two objects point to the same address, and if it points to the same address, it must be the same object and the object values are consistent

if (this = = AnObject) {

return true;

}

Returns False if the Parameter object is not a string data type, otherwise compares two string values, returns True if all equals, otherwise returns false

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;

}

Java Equals method 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.