Q: What is the Object class?
Answer: The Object class is stored in the java.lang package and is the ultimate parent class of all java classes (except the Object class). Of course, arrays also inherit the Object class. However, the interface does not inherit the Object class, and the Object class does not serve as the parent class of the interface.
Next, we analyze the object through examples
public class ObjectStu {/*Object class: the parent class of all classes in java, top-level class*equals: determine whether two objects are "equal"; *hashcode: Returns the hash code value of an object, which is an integer* Because the subsequent containers, such as hashset and hashMap, will deduplicate the elements; therefore equals and hashcode need to be rewritten at the same time; * The criteria to be met after rewriting: * When the result of equals is true, the hashcode values of the two objects are required to be equal; * When the result of equals is false, the hashcode values of the two objects are required to be unequal; (hashcodes can be equal, but if not equal, it can improve efficiency) *toString: Returns the string representation of the object * Function: It is convenient to view object properties in the program, and to debug code *debug learning: *1. Set breakpoints where there may be bugs*2.run as debug *3.f6, execute the current code; f5, enter the method of the current code in this sentence; f8, quickly jump to the next breakpoint, if there is no next breakpoint, run all the remaining code*equals: *1. Consistency: Use object A to call equals and object b to compare, the result of each time should be the same*2. Reflexivity: Object A calls equals to compare object b and object b calls equals to compare object a should be the same*/public static void main(String[] args) {Person per = new Person("Xiao Wang",10,150);Person per1 = new Person("Xiao Wang",20,140);Person per2 = per;System.out.println(per==per1);System.out.println(per.equals(per1));String str = "123";str.equals("123");str.hashCode();System.out.println(per.hashCode());System.out.println(per1.hashCode());System.out.println(per2.hashCode());System.out.println(per2.hashCode());System.out.println(per);System.out.println(per1);}}class Person {String name;int age;int height;public Person(String name, int age,int height) {super();this.height = height;this.name = name;this.age = age;}/*@Override public boolean equals(Object obj) { if(this==obj){ return true; } if(obj instanceof Person){ Person per = (Person)obj; if(this.name.equals(per.name)&&this.age==per.age){ return true; } } return false; }*///Rewrite hashcode/*@Override public int hashCode(){ return name.hashCode()+age*31+height*31; }*/@Override public int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + height;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Override public Boolean equals(Object obj) {if (this == obj) return true;if (obj == null) return false;if (getClass() != obj.getClass()) return false;Person other = (Person) obj;if (age != other.age) return false;if (height != other.height) return false;if (name == null) {if (other.name != null) return false;} else if (!name.equals(other.name)) return false;return true;}@Override public String toString() {return "Person[name="+name+",age="+age+"]";}}Summarize
The above is all the content of this article about the analysis of object class instances in Java, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Detailed explanation of Java internal test class code
Inheritance test code analysis in java
Example of code usage of stack (stack) in java
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!