Object common methods
Java interview often appear a topic, Object common methods. Let’s tidy it up for you.
Common methods of object include toString(), equals(), hashCode(), and clone().
toString
Default output object address.
public class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public static void main(String[] args) {system.out.println (new Person(18, "").toString()); } //output //me.tyson.java.core.Person@4554617c }Copy the code
You can override the toString method to print the object value according to the override logic.
public class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } @Override public String toString() { return name + ":" + age; } public static void main(String[] args) {system.out.println (new Person(18, "").toString()); } //output //Copy the code
equals
The default is to compare whether two reference variables refer to the same object (memory address).
public class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public static void main(String[] args) {String name = ""; Person p1 = new Person(18, name); Person p2 = new Person(18, name); System.out.println(p1.equals(p2)); } //output //false }Copy the code
We can override equals to determine whether age and name are equal:
public class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } @Override public boolean equals(Object o) { if (o instanceof Person) { Person p = (Person) o; return age == p.age && name.equals(p.name); } return false; } public static void main(String[] args) {String name = ""; Person p1 = new Person(18, name); Person p2 = new Person(18, name); System.out.println(p1.equals(p2)); } //output //true }Copy the code
hashCode
The information associated with the object is mapped to a hash value, and the default implementation hashCode value is converted based on the memory address.
public class Cat {
public static void main(String[] args) {
System.out.println(new Cat().hashCode());
}
//out
//1349277854
}
Copy the code
clone
Java assignment copies object references, and if we want to get a copy of an object, the assignment operation won’t do the trick. Object objects have a clone() method that implements pairs
A copy of each property in the image, but its visibility is protected.
protected native Object clone() throws CloneNotSupportedException;
Copy the code
So the premise of using clone for entity class is:
- Implement the Cloneable interface, which is a tag interface that has no methods of its own. This should be a convention. Did call clone method, determines the implement Cloneable interface, no implementation will throw exception CloneNotSupportedException Cloneable.
- Override the Clone () method and increase visibility to public.
public class Cat implements Cloneable { private String name; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Cat c = new Cat(); C. name = "programmer big bin "; Cat cloneCat = (Cat) c.clone(); C. name = "big bin "; System.out.println(cloneCat.name); } //output //Copy the code
Shallow copy
The reference type of the copy object and the original object refer to the same object.
In the following example, the Cat object contains a Person object. After clone is called, the cloned object and the Person of the original object reference the same object.
public class Cat implements Cloneable { private String name; private Person owner; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public static void main(String[] args) throws CloneNotSupportedException { Cat c = new Cat(); Person p = new Person(18, "programmer "); c.owner = p; Cat cloneCat = (Cat) c.clone(); P. etName (" DaBin "); System.out.println(cloneCat.owner.getName()); } //output //Copy the code
Deep copy
The reference types of the copy object and the original object refer to different objects.
In the following example, the clone function not only calls super.clone, but also calls the Clone method of the Person object (which also implements the Cloneable interface and overwrites the Clone method), thus implementing deep copy. As you can see, the value of the copied object is not affected by the original object.
public class Cat implements Cloneable { private String name; private Person owner; @Override protected Object clone() throws CloneNotSupportedException { Cat c = null; c = (Cat) super.clone(); c.owner = (Person) owner.clone(); // Copy the Person object return c; } public static void main(String[] args) throws CloneNotSupportedException { Cat c = new Cat(); Person p = new Person(18, "programmer "); c.owner = p; Cat cloneCat = (Cat) c.clone(); P. etName (" DaBin "); System.out.println(cloneCat.owner.getName()); } //output //Copy the code
\