Java advanced programming 02(Object)

Keywords: Java

Object class

Object is the top-level superclass of all classes. There are two methods that are often overridden by subclasses:
toString() and equals()

The Object class is the core class under the java.lang package. The Object class is the parent class of all classes. If a class does not explicitly inherit a parent class, it is a subclass of Object;

class Person { }
class Person extends Object { }

The final effect of the above two definitions is the same.
Structure diagram of Object class (Object provides 11 methods)

Let's analyze the methods one by one to see what the functions of these methods are:

1. clone()

Protect the method and realize the shallow replication of objects. This method can be called only when the clonable interface is implemented. Otherwise, a clonnotsupportedexception exception will be thrown.

2. getClass()

The final method returns an object of Class type and obtains the object through reflection.

3. toString()

This method is widely used. Generally, subclasses have coverage to obtain object information.

4. finalize()

This method is used to release resources. Because it is impossible to determine when the method is called, it is rarely used.

5. equals()

Are the contents of the comparison object equal

6. hashCode()

This method is used for hash lookup. The equals method is rewritten. Generally, the hashCode method is rewritten. This method is used in some collections with hash function.

7. wait()

The wait method is to make the current thread wait for the lock of the object. The current thread must be the owner of the object, that is, it has the lock of the object. The wait() method waits until it obtains a lock or is interrupted. wait(long timeout) sets a timeout interval. If the lock is not obtained within the specified time, it returns.

After calling this method, the current thread goes to sleep until the following events occur.

Another thread called the object's notify method.
Another thread called the object's notifyAll method.
Called by another thread interrupt Interrupt the thread.
The time interval is up.

At this point, the thread can be scheduled. If it is interrupted, an InterruptedException exception will be thrown.

8. notify()

This method wakes up a thread waiting on the object.

9. notifyAll()

This method wakes up all threads waiting on the object.

public class ObjectDemo {
	public static void main(String[] args) {
			//You can use Object to receive objects of all classes
			//The Object class belongs to the java.lang package, and all classes under the package
			//There is no need to import manually. The system will import automatically during compilation
			//Object per = new Person();
			  Object stu = new Student();
		    Object object = new Object();

Common Object methods:
toString(): retrieved object information
equals(): object content comparison

toString() method

      //toString() gets the object information and returns the string representation of the object
	     Student stu2 = new Student();
	      * When using object direct output, the default output is the output of an object in heap memory
	      * Memory address. If you need to output object information, override the toString method
	     Person person = new Person("Zhang San", 18);

The core purpose of toString is to obtain object information. As an important data type, toString performs "+" String splicing when all data types in java meet String.


For the above example, because the parent class of String is Object and the toString() method is defined in the parent class, any class contains the toString() method

After the object is instantiated, the toString () method can be called.
Each class contains the toString() method, but not every class overrides the toString() method of the Object class
In the source code of the Object class, it can be found that through reflection, the full class qualified name (package name + class name) and @ hexadecimal hash value string of the current Object are obtained. This is not to rewrite the content that toString() is a direct printout

equals method

equals(): object comparison

String Class object comparison uses equals()Method, actually String Class equals()The way is to override Object Class equals()method
  • For comparison of basic data types, use = = (for example, a == 3, b == 4, a == b, compare whether the values are equal)

  • Reference type data comparison: call the equals() method for comparison

Use equals() to compare whether the object contents are the same:

 	  equals()Method: object comparison
 	  String Class object comparison equals()Method, actually
 	  String Class equals()The way is to rewrite Object Class
 	 Basic data types are compared using ==(For example: a==3 b==4 a==b It's comparison
 	  Whether the values are equal.)
 	  Comparison between reference data types: calling equals()Methods are compared
        Person p1 = new Person("Zhang San", 18);
	    Person p2 = new Person("Zhang San", 18);
     For the above two objects with the same content, do it equals()Compare why
     Yes false?      
       Because it is called directly at this time equals()By default, the method compares two objects
       stay Object The comparison between the current object in the source code of the class and the passed object is still
       Address value(==) return boolean value   
       If you want to judge whether the contents of two object names of a class are the same
       We need to rewrite equals()method
class Person{
	private String name;
	private int age;
	//Parametric construction method
	public Person(String name, int age) {
		super(); = name;
		this.age = age;

	//If you want to use the toString method to output object information, you must
	//Override the toString() method in the Object class in the class
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
 For the comparison of the contents of the two objects of the above class, if you do not override it in the class
  Object of equals()Method compares whether the addresses of the two class objects are the same.
  Then, if you want to compare whether the contents of two class objects are the same, you need to
 Medium rewrite equals()Method. A variety of comparison options are available.
	public boolean equals(Object obj) {
		//1. Judge whether the addresses of the two current class objects are the same
		if (this == obj)
			return true;
		//2. Whether the object passed in is empty
		if (obj == null)
			return false;
		//3. Judge whether the two objects are of the same class
		if (getClass() != obj.getClass())
			return false;
		//Downward modeling
		Person other = (Person) obj;
		//4. Judge whether the contents of the two objects are the same
		if (age != other.age)
			return false;
		if (name == null) {
			if ( != null)
				return false;
		} else if (!name.equals(
			return false;
		return true;

class Student{}

Therefore, through the above examples, we can observe that when comparing data of reference type, the equals () method should be rewritten first, otherwise the heap memory address values of the two objects will not be equal.

Posted by Noggin on Sun, 26 Sep 2021 04:29:41 -0700