catalogue
Object class
We say that the object class is the parent class of all classes (all classes here include classes defined in java and classes created by yourself). All classes inherit object by default and are the ancestors of all classes
Objects of all our classes can be received by reference to the object class
Object object = new Demo1();
So why is there an object class?
I understand it this way: the emergence of the object class makes connections between other unrelated classes. In terms of inheritance, this is also reasonable. The most important thing is that the object class also defines many public methods for us to use
Then let's look at some of the more important methods
1. toString()
toString is to convert string. Let's give an example first
public class Car { private String color; //colour private String brand; //brand public Car(String color, String brand) { this.color = color; this.brand = brand; } public static void main(String[] args) { Car car = new Car("white","bmw"); System.out.println(car); } }
Design a car class, provide members and constructs for it, and then we output the object directly
Results:
So here's a question: can an object be output directly when it is in memory?
Certainly not, but the reason why we can output here is that we call the toString() method in the object class by default
Let's look at how to do it in the jdk source code
In the jdk source code, toString() returns the class name + "@" + hash value, so it is not difficult to understand why the object output is like this
If we don't want to output like this, we need to override the toString() method in the object class in our class, for example:
@Override public String toString() { return "Car{" + "color='" + color + '\'' + ", brand='" + brand + '\'' + '}'; }
Let's try again. The result is:
In the jdk, some classes have also rewritten the toString() method, such as the String class. Although the String is a reference type, it outputs the content
The above is an introduction to toString()
2. equals()
Let's first look at how equals() is implemented in the source code
We can see that the equals() method in the jdk source code is used to compare "= =", that is, to compare the addresses of two references
Another example:
Car car1 = new Car("white","bmw"); Car car2 = new Car("white","bmw"); System.out.println(car1.equals(car2));
You don't have to think about it. The output here must be false, because we refer to two different objects. The equals() method here is also the equals() method in object. The two results of = = comparison must be different
How can we make the result the same (the content of the comparison object). The answer is definitely: rewrite the equals () method: for example:
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Car car = (Car) o; return Objects.equals(color, car.color) && Objects.equals(brand, car.brand); }
In this way, the content of the object is compared, and the result is true
In some classes provided by jdk, such as String class, it has overridden the equals method. When we compare two strings, it will compare the contents by default, as follows:
String s1 = "aaa"; String s2 = "aaa"; System.out.println(s1.equals(s2)); String s3 = new String("111"); String s4 = new String("111"); System.out.println(s3.equals(s4));
Both results are true because the String class has overridden the equals() method of the Object class and compared the content
3. hashCode()
jdk source code:
You can see that the return value of this method is int, that is, get the hash value. In addition, it is decorated with native. It is a local method, that is, it is not implemented in java language
We will also rewrite the hashCode() method. The rewritten method will generally get the hash value according to the content of the object. Through the hash value, we can compare whether the two objects are the same
Rewrite the hashCode() method through the above example:
@Override public int hashCode() { return Objects.hash(color, brand); }
Car car1 = new Car("white","bmw"); Car car2 = new Car("white","bmw"); System.out.println(car1 + ":" +car1.hashCode()); System.out.println(car2 + ":" +car2.hashCode());
result:
You can see that the contents of our two objects are the same, and the hash value obtained by the rewritten hashCode() method is the same
It should be noted that although the values of the hashCode method rewritten in the String class are different, the hash values obtained may be the same, such as "call" and "re place"
String s1 = "conversation"; String s2 = "Heavily"; System.out.println(s1.hashCode()); System.out.println(s2.hashCode());
Results:
How can we distinguish when the same situation occurs? Then we can call equals() again to compare
Therefore, we generally try to rewrite equals() and hashCode() together, because in some comparison problems, we usually judge the hash value of the two objects first, and then call equals() for comparison. For example, this judgment principle is used when the hashMap key cannot be repeated