);System.out.println (p1); Output 16 Binaryperson P2 = new Person ("Zhang Shan", 20);System.out.println ("Hascode of the object of the P1:" +p1.hashcode ());//output is a decimal dataSystem.out.println ("Hascode of the object of the P1:" +p2.hashcode ());//output is a decimal dataSystem.out.println ("-----------------------");System.out.println (P1 = = p2);System.out.println ("Two objects are equal:" +p1.
The Equals and Hashcode methods in Java are in object objects, so each object has these two methods, and most of the time we need to override both methods in order to implement the specific requirementsThe Equals and Hashcode methods are commonly used in the same class for c
The following is summarized from "effective Java".
1. When to rewrite equals ()
When a class has its own unique concept of "logical equivalence" (different from the concept of object identity).
2. Design Equals ()
[1] Use the instanceof operator to check whether the argument is the correct type.
[2] For each "critical field" in a class, check the field in th
The difference between = =, Equals, Hashcode ():Basic data types: compare with = =, compare their valuesComposite data types: when compared with = =, the comparison is that they are stored in memory address, unless it is the same new object, they are the result of the comparison is true, otherwise false.The Equals () method is defined in the object base class, an
The Hashcode () method of object in Java is not overridden, then the Hashcode () method returns the memory address ...
The ToString () method, as defined by the default object object, returns the Hashcode () return value of the package name + class name +@+16.
In our actual development, for example, the author defines
I. Overview
1, Concept = =: This operator generates a Boolean result, which computes the relationship between the operands ' values EQUAL3 intention: $ Two objects n%sp;content is EA6 the same hashcode: the HA%%c value of the object, for 7E 7 The object in the Ha%%5 table in the index bit 뽮, it is actually 8b8 int type integer two, relational operator = =
1, the value of the operand
In Java, there are eigh
; result = * result + linenumber; return result; } */ //if an object is not constantly changing, and the cost is relatively large, you have to consider it. Hash code is cached inside the object//with volatile modifiers, the thread will read the variable's most-modified value each time it uses the variable. Private volatile inthashcode; @Override Public inthashcode () {intresult =hashcode; if(Result = = 0) {result= 17; Result= *
Hashcode are hash codes for each object and are the identities used in HashSet to allocate object storage. Each equal object must have the same hashcode. Therefore, overriding the Equals method will override Hashcode ().References are the same, hashcode must be the same. And
= =: Compares the address or value of variables in the Java stack local variable table for equality.equals: The address of the comparison variable refers to whether the object is the same object in the Java heap.Hashcode: A hash value is returned through a specific algorithm through the storage address of the object in the JVM memory, mainly with the addition and deletion of HashSet HashMap.Public native
[Valid Java] 5. overwrite the hashcode and mongoshashcode when overwriting equals.
Package cn. xf. cp. ch02.item9; import java. util. hashMap; import java. util. map; public class PhoneNumber {private final short areaCode; private final short prefix; private final short line
Why must I rewrite hashcode while rewriting equal?Hashcode is different integers generated by the compiler for different objects. According to the equal method, if two objects are equal (equal), the two objects must call hashcode to produce the same integer result, that is, if equal is true, hashcode must be true, equa
that determines whether two objects are equal is:The first step, if hashcode () is equal, look at the second step, otherwise not equal ;The second step is to see if equals () is equal, and if they are equal, the two obj are equal or not equal. 1, firstEquals () andHashcode () These two methods are all fromInherited from the object class.Equals () is a comparison of the address values of two objects (that i
From http://blog.sina.com.cn/s/blog_4a5ca024010008tj.html below is the text
Xiao Cui's translation of Virgo "Equals method and Hashcode method" (2007-06-22-21:04:42
Cui (folk also called Tri, namely CY, see chat Record series), Hunan people, more than 20 years old, unmarried. Once aj*q*051 class team leader, the existing translation of the "Equals method and
;
} else if (!lastname.equals (other.lastname)) r Eturn false;
return true;
}? So by overriding the Equals () method in a class, we can compare whether different objects are equal under the same class.
2.Hash algorithm principle and hashcode deep understandingThere are two kinds of collection in Java, one is list, the other is set. The elements in the l
Tags: blog HTTP Io OS ar Java for SP Div
First, I need to explain that you can rewrite these two methods in the class we write. At this time, from the syntax perspective, they do not matter.
In the object
public native int hashCode();
public boolean equals(Object obj) { return (this == obj);}The two rules are in the J
Tags: equals hashcode native object string
First, I have to explain that you can repeat these two methods in the class we write. At this time, from the syntax perspective, they do not matter.
In the object
public native int hashCode();
public boolean equals(Object obj) { return (this == obj);}The two rules are
Why the equals and hashcode methods are used?Equals: Sometimes we need to closely compare whether two objects are equal (manually called by ourselves)Hashcode: it is because we want to save the unique object to the Collection (or there cannot be duplicate values in the actual collection, and we also need to compare whe
In this article, I will show you my understanding of the hashcode and equals methods. I will discuss their default implementations and how to rewrite them correctly. I will also use the Toolkit provided by Apache commons for implementation.
Directory:
Hashcode () and equals () Usage
Override default implementation
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.