Java in-depth research [1. object Class], in-depth research on object

Source: Internet
Author: User

Java in-depth research [1. object Class], in-depth research on object

I. Overview
The Object class is the ancestor of all Java classes. Each class uses an Object as a superclass. All objects (including arrays) implement this class method.

Reference English:
* Class {@ code Object} is the root of the class hierarchy.
* Every class has {@ code Object} as a superclass. All objects,
* Including arrays, implement the methods of this class.

Meaning:
Class hierarchy


Ii. Methods in the object class

1. equals
Determine whether two objects are equal

2. hashCode ()
Returns the hash value of the object.

3. getclass ()

Return thisObject.

4. clone ()

Create and return a copy of this object.

 

Iii. Detailed explanation

1. equals ()
Reflexive: returns true for any non-null reference value x and x. equals (x.
Symmetric Ric: For any non-null reference values x and y, x. equals (y) returns true only when y. equals (x) returns true.
Transitive: For any non-null reference values x, y, and z, if x. equals (y) returns true, and y. equals (z) returns true, then x. equals (z) should return true.
Consistent: For any non-null reference values x and y, call x multiple times. equals (y) always returns true or false, provided that the information used in the equals comparison on the object is not modified.
Returns false for any non-null reference value x, x. equals (null.
The equals method of the Object class implements the maximum possible equality relationship between objects. That is, for any non-null reference values x and y, if and only when x and y reference the same Object, this method returns true (x = y has a value of true ).
Note: When this method is overwritten, it is usually necessary to override the hashCode method to maintain the conventional protocol of the hashCode method, which declares that equal objects must have equal hash codes.

The above features must be followed when rewriting the equals method, otherwise unexpected problems may occur. Here we will not give an example.

Reference English:
* Note that it is generally necessary to override the {@ code hashCode}
* Method whenever this method is overridden, so as to maintain
* General contract for the {@ code hashCode} method, which states
* That equal objects must have equal hash codes.

Meaning:
Non-null reference value
Multiple invocations multiple calls

2. hashcode ()
Returns the hash value of the object. This method provides some advantages for the hash table, such as the hash table provided by java. util. Hashtable.
The general protocol of hashCode is:

  • During Java application execution, when calling the hashCode method multiple times on the same object, the same integer must be returned consistently, provided that the information used in the equals comparison on the object is not modified. This integer does not need to be consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals (Object) method, the hashCode method must be called on each Object of the two objects to generate the same integer result.
  • The following situations are not required. lang. object) method. If the two objects are not equal, calling the hashCode Method on any of the two objects will certainly generate different integer results. However, programmers should know that generating different integer results for unequal objects can improve the performance of the hash table.

In fact, the hashCode method defined by the Object class does return different integers for different objects. (This is generally achieved by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language .)

3. getclass ()

Return thisObject. ReturnedClassThe object is composed ofstatic synchronizedObject locked by the method.

4. clone ()

Create and return a copy of this object. The exact meaning of a copy may depend on the class of the object. This is intended for any objectX, Expression:

x.clone() != x

True, expression:

x.clone().getClass() == x.getClass()

OrTrue.

 

ObjectClassCloneMethod to perform a specific copy operation. First, if the class of this object cannot implement the interfaceCloneableWill throwCloneNotSupportedException. Note that all arrays are considered as implemented interfaces.Cloneable. Otherwise, this method will create a new instance of the class of this object, and initialize all fields of this object using the content of the corresponding fields of this object just like by allocation; the content of these fields is not self-copied. Therefore, this method executes the "superficial copy" of the object, instead of "deep copy.

 

ObjectClass itself does not implement interfacesCloneableInObjectObject.CloneMethod will throw an exception at runtime.

 

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.