Home>

The object class is in the java.lang package and is the ancestor of all java classes.Every class in java extends from it.

If the java class is not explicitly specified when defining the java class,Then it inherits the object class by default. E.g:

public class demo {
  //...
}

It is actually a short form of the following code:

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

In java, only primitive types are not objects,For example, numeric, character, and Boolean values ​​are not objects.All array types,Both object arrays and primitive types are inherited from the object class.

The object class defines some useful methods,Since it is a root class,These methods exist in other classes,It is usually overloaded or overwritten,Realized their specific functions.

equals () method

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

 public boolean equals (object obj)

E.g:

obj1.equals (obj2);

In Java, the basic meaning of data equivalent means that the values ​​of two data are equal.When comparing by equals () and "==",Reference type data compares references,Memory address,Basic data types compare values.

note:

The equals () method can only compare reference types,"==" can compare reference types and basic types.

When comparing using the equals () method,For classes file, string, date, and wrapper classes,It compares type and content regardless of whether the reference is the same instance.

When using "==" for comparison,The data type on both sides of the symbol must be the same (except for data types that can be automatically converted), otherwise the compilation errorThe two data to be compared by the equals method need only be reference types.

hashcode () method

A hash code is a value obtained from an object according to a certain algorithm.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 fast find and other operations in the collection.Can also be used for comparison of objects.

The rules for hashcode in java are as follows:

During the same application execution,Calling hashcode () on the same object must return the same integer result-provided that the information compared by equals () has not been altered.As for the results of calls made by the same application in different execution periods,No need to be consistent.

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 considered unequal by the equals () method,Then calling hashcode () on these two objects doesn't have to produce different integer results.However programmers should be aware thatProduces different integer results for different objects,It is possible to improve hashtable (we will learn later,(A class in the collections framework).

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.In the java specification,Generally, the equals () method should be overridden with the hashcode () method.

tostring () method

The tostring () method is another important method defined in the object class.Is the string representation of the object,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 of the current object.However, it has been rewritten in some subclasses (such as string, date, etc.). You can also override the tostring () method in user-defined types as needed to return more applicable information.

In addition to explicitly calling the object's tostring () method, when performing a string and other type of data connection operation,The tostring () method is called automatically.

  • Previous Analysis of transaction usage examples in C #
  • Next Problems caused by the installation order of IIS and net framework 40 (re-registering net 40)