Java-equal and Hashcode

Source: Internet
Author: User

The paper came to the end of shallow, I know this matter to preach-Lu Ask the canal that is clear so, for have fountainhead come--Zhu Xi


Equals () is used to determine whether two objects are equal, and in the object class, Equals (Object obj) is determined by determining whether the addresses of two objects are equal (or not the same object) to differentiate between them, the source code:


{
(= = obj) ;
}
Because the object class is the parent class for all classes, using equals (Object obj) in a subclass is used to determine whether two objects are the same object.You need to override the Equals () method when you are judging whether the contents of two objects are equal. There are also commonly used methods are "= =", the equal sign is also the judgment of the object reference.
The example of overriding equals () and not overriding equals () contrasts:
Public classHello {

Public static voidMain(String []args)
{
Person1 person1=NewPerson1 (Ten, "Tian");
Person1 person2=NewPerson1 ( -, "Tian");
if(Person1.equals (Person2))
{
System. out. println ("Equal");
}
Else
{
System. out. println ("Don't want to Wait");
}

if(Person1==person2)
{
System. out. println ("Equal");
}
Else
{
System. out. println ("Not Equal");
}
Person2 person22=NewPerson2 (Ten, "Tian");
Person2 person23=NewPerson2 (Ten, "Tian");
if(Person22.equals (PERSON23))
{
System. out. println ("Equal");
}
Else
{
System. out. println ("Not Equal");
}
if(PERSON22==PERSON23)
{
System. out. println ("Equal");
}
Else
{
System. out. println ("Not Equal");
}
}
Public Static ClassPerson1
{
int Age;
Stringname;
PublicPerson1 (intAge,String name)
{
This. Age=age;
This.name=name;
}
}
Public Static ClassPerson2
{
int Age;
Stringname;
PublicPerson2 (intAge,String name)
{
This. Age=age;
This.name=name;
}
@Override
Public Booleanequals(Object object)
{
if(object==NULL)
{
return false;
}
if( This==object)
{
return true;
}
if( This. GetClass ()!=object.getclass ())
{
return false;
}
Person2 person2= (Person2) object;
returnPerson2.name. Equals (name) &&person2. Age== Age;
}
}
}
in the object class, the Hashcode () method is a local method that returns the address value of an object, and the Equals () method in the object class compares the address values of two objects, so equals () is equal, which describes the equality of the two object address values, So the hashcode () is equal.
because the Equals () and hashcode () two functions are overridden in a common function, the hashcode () function is generally overridden for the Equals () function, or equals () is equal and hashcode () Unequal conditions. In the collection class, there is a case where hashcode () is equal in Hashset,hashmap and Hashtable, and equals () is unequal, because both hashcode () and Equals () are overridden.

Copyright Notice: Knowledge is to share, technology lies in communication, please leave a blogger's link on the reprint.

Java-equal 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.