Four. Java inheritance and polymorphism 10. Java Object class

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 defining a Java class, if no indicated parent class is displayed, the Object class is inherited by default. For example:

    1. Public class Demo{
    2. // ...
    3. }

is actually shorthand for the following code:

    1. Public class Demo extends Object{
    2. // ...
    3. }

In Java, only the base type is not an object, such as numeric, character, and Boolean values are not objects, all array types, whether an object array or an array of primitive types, are inherited from the object class.

The Object class defines some useful methods, because they are root classes, which exist in other classes and are generally 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:

    1. Obj1. equals(Obj2);

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

Attention:

    • The Equals () method can only compare reference types and "= =" to compare reference types and basic types.
    • When comparing with the Equals () method, the class File, String, Date, and wrapper classes are comparison types and content regardless of whether the reference is the same instance.
    • When you compare with "= =", the data types on both sides of the symbol must be the same (except for data types that can be automatically converted), otherwise the compilation will fail, and the two data compared with the Equals method is only a reference type.
Hashcode () method

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

The Hashcode () method is primarily used to implement operations such as Quick Find in a collection, or to compare objects.

In Java, the rules for Hashcode are as follows:

    • During the same application execution, Hashcode www.120xh.cn () is called on the same object, and the same integer result must be returned-provided that the information compared by equals () has not been altered. There is no need to agree on the results of the same application's invocation at different execution periods.
    • 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 producing different integer results for different objects is likely to improve the efficiency of Hashtable (a class in the collection framework, which is later learned).


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 overridden that the Equals () method should overlap 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 the 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 it in some subclasses (such as String, Date, and so on), or optionally overrides the ToString () method in a user-defined type to return more appropriate information.

In addition to the ToString () method of an explicitly called object, the ToString () method is called automatically when a String is connected to another type of data.

The above methods are often used in Java, here is a brief introduction to the object class and other classes to understand, detailed instructions please refer to the Java API documentation.

Four. Java inheritance and polymorphism 10. 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.