Home  >  Article  >  Java  >  Summary of common methods of parent class Object in Java

Summary of common methods of parent class Object in Java

高洛峰
高洛峰Original
2017-01-17 16:56:391130browse

Introduction

Object class: This class java.lang.java is the parent class that all classes inherit by default

Three methods commonly used in the Object class: toString () , equal() , hashCode()

1. toString() method object’s self-description, object’s self-introduction

In the process of object’s self-description, due to the use of get() It is more troublesome to print instance variables. For simplicity, use toString() .

Public String toString(){
 
 
Return “学好” + getNo() + “姓名” + getName();
 
}


##System.out.println(s); which is equivalent to System.out.println(s .toString());

In printing:

a. Directly print the value when the basic data type is used

b. Call toString() when referencing the data type in printing

If the toString() method in the Object class is not overridden, the object is printed directly, and the result obtained on the console is:

Includes the full name@hashCode of the object

2. The equals() method must rewrite hashCode() when rewriting equals().

Equals() compares two objects, or the contents of two objects; hashCode() is the internal content of the object. Address is an integer.

==What is the difference between equals()?

Answer: ==The actual comparison is binary, the basic data type is compared with numerical values, and the reference data type is compared with address

Equals() is divided into equals() and String of the Object class The equals() of the class, the equals() of the Object class is equivalent to == comparison of reference types; the equals() of the String class overrides the equals() method in the Object class, and compares literal values.

Instanceof determines the class name, or you may use reflection

The reference variable name of the object instanceof class name is the expression, if the reference variable name of the object is the class or base class object of the following class name The return value is still true.

Customized equals() method:

1. The types are the same

2. Define your own comparison rules

public boolean equals(Object object){ //重写Object的equals()方法
 
EqualsTest equalsTest = (EqualsTest) object;
 
if(equalsTest.getClass() == EqualsTest.class){ //???????????
 
if((this.name==equalsTest.name)&&(this.price== qualsTest.price)){
 
return true;
 
}else{
 
return false;
 
}
 
}else{
 
return false;
 
}
 
}


3. The hashCode() method is used to speed up search and implement index search (query algorithm, storage algorithm)

Public int hashCode(){
 
Return this.name.hashCode()*13 + new Interger(getAge).hashCode()*12;
 
}


Two parts of hashCode Principle:

1. When the equals() method comparison return value of two objects is true, then their hashCode() method must also ensure that the return value is the same.

2. The attributes used in objects to compare using equals() should be used to calculate hashCode().

hashCode() Hash algorithm

Requirements:

1. The return value of hashCode() is the same, and the return value of equals() must also be the same.

2. Rewrite what attributes are used by hashCode(), and what attributes should be used in the equals() method.

Note: hashCode is the same, equals is not necessarily the same.

hashCode is used to determine the folder, and equals is used to determine the file.

If you do not override the hashCode() method, the default hashCode() method is calculated based on the address of the object (the key value is the address of the object)

If you override hashCode() The method cannot be used to determine whether it is the same object.

You can use System.identityHashCode (the reference name of the object). If the hashCode is the same, it is the same object.

System.out.println(System.identityHashCode(object reference name));

If there is no hashCode(), the hashCode of the object is calculated from the object address.

System.out.println(Object reference name.hashCode());

Note: Whether the element exists in the HashSet collection and the deletion operation depends on the element's hashCode() and equals( )

Summary

The above are the three common methods of the parent class Object in Java. I hope it can be helpful to everyone. If you have any questions, you can leave a message to communicate. Thank you for your support. PHP Chinese network support.

For more related articles summarizing the common methods of the parent class Object in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn