Java basics of Object class _java

Source: Internet
Author: User

The Object class is in the Java.lang package and is the ancestor of all Java classes, and each class in Java is extended by it.

When you define a Java class, the Object class is inherited by default if the specified parent class is not displayed. For example:

 public class demo{
   //...
 }

is actually shorthand for the following code:

 public class Demo extends object{
   //...
 }

In Java, only the base type is not an object, for example, values, characters, and Booleans are not objects, and all array types, whether an object array or a base type array, inherit from the object class.

The Object class defines some useful methods that, because they are root classes, exist in other classes and are typically overloaded or overwritten to achieve their specific functions.

Equals () method

The Equals () method in the object class is used to detect whether an object is equivalent to another object, and the syntax is:
public boolean equals (Object obj)
For example:

Copy Code code as follows:

Obj1.equals (OBJ2);

In Java, the basic meaning of data equivalence is that the values of two of data are equal. When comparing by Equals () and "= =", reference type data compares the reference, that is, the memory address, and the base data type compares the value.

Attention:

The equals () method can only compare reference types and "= =" to compare reference types and base types.
• When comparing using the Equals () method, the class File, String, Date, and wrapper class are compared to the type and content without regard to whether the reference is the same instance.
• When comparing with "= =", the data types on both sides of the symbol must be the same (except for the data types that can be automatically converted), or compile errors, and the two data that is compared with the Equals method as long as it is a reference type.

Hashcode () method

hash code (HASHCODE) is a numerical value obtained by an object according to a certain algorithm, and the hash code is not regular. If x and Y are different objects, X.hashcode () and Y.hashcode () are basically not the same.

The Hashcode () method is mainly used to implement quick lookup in the collection, and can also be used for object comparison.

In Java, the rules for Hashcode are as follows:

• Call Hashcode () on the same object during execution of the same application and must return the same integer result-if the information compared by equals () has never been altered. As for the invocation results of the same application at different execution periods, there is no need to be consistent.
• If two objects are considered equal by the Equals () method, calling Hashcode () on both objects must obtain the same integer result.
• If two objects are considered unequal by the Equals () method, calling Hashcode () on both objects does not have to produce different integer results. However, programmers should be aware that different integers result in different objects, and it is possible to elevate the efficiency of Hashtable (which later learns a class in the set framework).

Simply put: If two objects are the same, their hashcode values must be the same, and if two objects have the same hashcode values, they are not necessarily the same. In the Java specification, it is generally stated that overriding the Equals () method should cover the Hashcode () method.

ToString () method

The ToString () method is another important method defined in the object class, which is the string representation of the objects, and the syntax is:
Public String toString ()
The return value is a String type that describes information about the current object. The ToString () method implemented in the object class returns the type and memory address information for the current object, but overrides in some subclasses (such as String, Date, and so on) or overrides the ToString () method in the user's custom type as needed to return more applicable information.

In addition to explicitly calling the object's ToString () method, the ToString () method is automatically invoked when a String is connected to other types of data.

The above several methods, in Java is often used, here only for a brief introduction, let you have an understanding of the object class and other classes, detailed instructions please refer to the Java API documentation.

The above mentioned is the entire content of this article, I hope you can enjoy

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.