Java Object class

Source: Internet
Author: User

Java.lang.Object

The Java.lang package does not need to display the import when it is used, and is automatically imported by the compiler at compile time.

The object class is the root of the class hierarchy, and all classes in Java are fundamentally inherited from this class.

The object class is the only class in Java that does not have a parent class.

All other classes, including standard container classes, such as arrays, inherit the methods in the object class.

methods in the object class

  Construction method: Public Object ()

class overview in the documentation:

Each class in Java has these methods defined in the object class.

protected Object Clone ()

Creates and returns a copy of this object.

The description in the object class is:

Protected Object Clone ()

Throws Clonenotsupportedexception

This method is quite special:

First, the class using this method must implement the Java.lang.Cloneable interface, or the clonenotsupportedexception exception will be thrown.

The Cloneable interface does not contain any methods, so you can implement it by simply adding a implements statement to the class declaration.

The second special place is that this method is protected-modified, and the Clone () method needs to be written as public in order for the code outside the class to be called.

boolean equals (Object obj)

Indicates whether some other object are "equal to" this one.

the "= =" operator Determines whether two references point to the same object.

For the Equals () method of the object class, it determines whether the invocation of the Equals () method is consistent with the reference passed in, that is, whether the two references point to the same object.

The Equals () method in the object class is as follows:

Boolean equals (Object obj) {    return (this= = obj);}   

the Equals () method in the object class is equivalent to = =.

The inherited class implements the Equals () method to compare the equality of two objects only if the class that inherits the object is overwrite (override) the Equals () method, so that the Equals () method differs from = =.

 

  equals () The method needs to have the following characteristics:

  reflexive (reflexive): any non-null reference x,x.equals (x) returns to true.

  symmetry (symmetric): any non-null reference x and Y,x.equals (y) return true if and only if Y.equals (x) returns True.

  transitivity (transitive): any non-null reference x and Y, if X.equals (y) returns True, and Y.equals (z) returns True, then X.equals (z) returns TRUE.

  Consistency (consistent): multiple invocations of two non-null references x and y,x.equals (y) should maintain consistent results (provided that there is no modification of x and Y for comparison between multiple comparisons).

  contract : x,x.equals (NULL) should return FALSE for any non-null reference.

And when you overwrite the Equals () method, you should also overwrite the Hashcode () method, and vice versa.

int hashcode ()

Returns a hash code value for the object.

After you overwrite (override) the Equals () method, you must also overwrite the Hashcode () method, and vice versa.

This method returns an integer value (hash code value), and if two objects are judged equal by the Equals () method, they should have the same hash code.

  The Hashcode () method of the object class returns different values for different objects, and the hashcode value of the object class represents the address of the objects.

hashcode the general contract (conditions to be met) are as follows:

1. During one execution of a Java application, if the information used by the object for the equals comparison is not modified, multiple calls to the Hashcode () method on the same object should return the same integer value.

In multiple executions of the application, this value does not need to be consistent, that is, each execution is maintained with its own distinct value.

2. If Equals () determines that two objects are equal, their hashcode () method should return the same value.

3. There is no mandatory requirement if equals () determines that two objects are not equal, then their hashcode () method should return different values.

That is, two objects return false with the Equals () method, and their hashcode can be the same or different. However, you should be aware that generating two different hashcode for two unequal objects can improve the performance of a hash table.

String toString ()

Returns a string representation of the object.

When a reference is printed, such as calling System.out.println (), the ToString () method of the object is called automatically, and the return value of the ToString () method of the object referred to is printed out, since each class is directly or indirectly inherited from object. Therefore, each class has the ToString () method.

The ToString () method in the object class is defined as follows:

Public String toString () {    return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ());}  

Reference Recommendations

Official documentation and source code SRC in the JDK.

Document download for help format: http://www.allimant.org/javadoc/index.php

Recommended Leather House: http://www.ppurl.com/

Java Object class

Related Article

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.