Introduction to the Object class in Java programming

  • 2020-04-01 04:10:03
  • OfStack

The Object class is in the java.lang package and is the ancestor of all Java classes, from which every class in Java is extended.

If there is no parent class shown when defining a Java class, then the Object class is inherited by default. Such as:


public class Demo{
  // ...
}

This is actually a shorthand form of the following code:


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


In Java, only primitive types are not objects, such as numeric, character, and Boolean values, and all array types, whether Object arrays or primitive types, are inherited from the Object class.

The Object class defines some useful methods. Since it is the root class, these methods exist in other classes and are generally overridden or overridden to achieve their specific functions.
The equals () method

The equals() method in the Object class is used to check whether an Object is equivalent to another Object. The syntax is:


  public boolean equals(Object obj)


Such as:


obj1.equals(obj2);


In Java, the basic meaning of data equivalence is that two data values are equal. When comparing through equals() and ==, reference type data compares references, that is, memory addresses, and base data types compare values.

Note:
The equals() method can only compare reference types, and "==" can compare reference types and primitive types.
When comparing with the equals() method, for the classes File, String, Date, and wrapper, the type and content are compared regardless of whether the reference is to 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), otherwise compilation will fail, and the two data that are compared with equals are all reference types.
HashCode () method

HashCode is a value obtained by an object according to a certain algorithm. The hashCode has no rules. If x and y are different objects, x.ashcode () and y.ashcode () are basically not the same.

The hashCode() method is primarily used to perform operations such as quick lookups in collections, and can also be used to compare objects.

In Java, hashCode is specified as follows:
Calling hashCode() on the same object during the execution of the same application must return the same integer result -- provided that the information compared to equals() has not been changed. There is no need to be consistent with the results of calls made by the same application in different execution periods.
If two objects are considered equal by the equals() method, then calling hashCode() on both objects must get the same integer result.
If two objects are not considered equal by the equals() method, calling hashCode() on the two objects does not have to produce different integer results. Programmers should be aware, however, that generating different integer results for different objects might improve the efficiency of hashTable (a class in the collection framework, as we'll see later).

Simply put: if two objects are the same, then their hashCode values must be the same; If two objects have the same hashCode value, they are not necessarily the same. According to the Java specification, the method hashCode() should be overridden along with equals().
The toString () method

The toString() method is another important method defined in the Object class, which is the string representation of the Object. The syntax is:


  public String toString()


The return value is of type String, which describes information about the current object. The toString() method implemented in the Object class returns the type and memory address information of the current Object, but is overridden in some subclasses (such as String, Date, and so on), or you can override the toString() method in user-defined types as needed to return more applicable information.

In addition to explicitly calling the toString() method of the object, the toString() method is automatically called when String is concatenated with other types of data.

The above several methods, in Java is often used, here is only a simple introduction, let you know about the Object class and other classes, detailed please refer to the Java API documentation.


Related articles: