Preface
Unlike C++, Java language is a single-root inheritance structure language, which means that all classes in Java have a common ancestor. This ancestor is the Object class.
The Object class is called God class, also known as the ancestor class. When defining a Java class, if the parent class is not specified, the Object class will be inherited by default. With Java's up-type conversion, a lot of work can be done with the help of the Object class.
The structure of the object class
Methods of Object class
In the Object class, there are several commonly used methods, such as getClass(), toString() and equals(). Their implementation in Object.java is as follows: (The source code of Object.java is in src.zip in the Java installation directory, and its directory structure is src/java/lang/Object.java).
public final native Class<?> getClass();public boolean equals(Object obj) { return (this == obj);}public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode());}For getClass(), it can get classes at runtime.
equals() is usually used for comparison. In the Object class, it compares references and returns true only if the references are the same. But this is not the case of comparison we want, for example, the comparison of strings is to compare whether each character is the same. Then the equals() method will be rewritten in the String class. The implementation of the equals() method in the String class is as follows: (The source code of String.java is in src.zip in the Java installation directory, and its directory structure is src/java/lang/String.java).
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false/; i++; } return true; } } return false;}The comparison of strings has been made several times:
1. First, judge that the references are the same, and if they are the same, return true;
2. Determine whether the incoming object is a String. If it is not a String, it will directly return false. There is no need to compare it, how can the dog and human be compared;
3. Compare the length of the string. If the lengths of the two strings are different, there is no need to compare them. Different lengths are obviously different;
4. If the above comparison is not true, then a character-by-character comparison is made.
For comparison of two objects, we usually need to rewrite the equals() method like the equals() method in the String class.
The third method toString() outputs the current runtime class and a HashCode in the Object class by default, which is not particularly helpful to us. For classes written by ourselves, you can output all attributes in the class. After rewriting the toString() method, you can directly use System.out.println() to pass the object name, and you can call the object's toString() method by default.
As a beginner, you have to constantly write code, and record a lot of what you have learned to deepen your impression. You also have to search easily when you forget, and record it here and make it convenient for you to make it convenient for you at any time!
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.