Article Directory
Methods in Object
The Object class is the parent class of all classes, and all classes inherit Object by default at the bottom
equals returns true, hashCode must be the same, so hashCode and equals must be rewritten at the same time (applied in the set)
equals(Object obj)
: Determine whether the objects are the same
Rewriting equals() method must rewrite hashCode()
-
When overriding equals(), if the object contains object properties
-
Or initialize the object properties when creating the object
-
Or judge whether the object attributes are all null in the equals() method;
@Override public boolean equals(Object o) { if (this o) return true; if (o null || getClass() != o.getClass()) return false; Person person = (Person) o; return age person.age && gender person.gender && (namenull && person.namenull)|| name.equals(person.name); }
-
-
What is returned is the hash code value of the object
-
Different objects have different hash code values
-
Hash code value: Hash code value is almost never repeated -----Hash code value must be unique-----represents the address value of the object
- Value range: as large as the int range, the hash value is not negative [0-41 billion]
- Hash distribution: evenly distributed
toString()
: Return the object as a string
Do not rewrite: the toString() in the Object class is called, and the splicing address value of the object is returned
Rewrite: call the rewritten toString() method
System.out.println(demo3);
AndSystem.out.println(demo3.toString());
the same effect- The bottom layer defaults to let the object call toString() in the Object class to return the address value of the object
clone()
: Create and return a copy of this object
-
Need to inherit the
Cloneable
interface (this interface is only used as a mark) to call theclone()
method -
clone()
The method can only be called in the Object subclass itself. Calling methods of other objects in other classesclone()
will cause an error. Reason: protected feature -
CloneNotSupportedException
: Cloning does not support exceptions (does not implement the cloneable interface)
Clone code example
package cn.tedu.objectdemo; /** * Cloneable接口没有内容,仅仅只是作为一个标记 * 类实现Cloneable接口,类产生的对象就能支持克隆操作 */ public class ObjectDemo1 implements Cloneable{ int i = 6; public static void main(String[] args) throws CloneNotSupportedException { //创建对象 ObjectDemo1 od = new ObjectDemo1(); //调用克隆方法 ObjectDemo1 od1 = (ObjectDemo1) od.clone(); //克隆对象调用属性 System.out.println(od1.i); System.out.println(odod1); } }
finalize()
: Notify the system for recycling
When the garbage collection determines that there is no reference to the object, it is called by the garbage collector on the object.
getClass()
: Returns the class of Object runtime
Package name + class name: full path name
Which object is called returns the full path name of the object
Example code
Student class
package demoObject;
public class Student {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(int age) {
super();
this.age = age;
}
public Student() {
}
@Override
public String toString() {
return "Student [age=" + age + "]";
}
//重写hashCode和equals方法------------------------------------
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
return result;
}
@Override
public boolean equals(Object obj) {
if (this obj)
return true;
if (obj null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
return true;
}
}
DemoObject test class
package demoObject;
public class DemoObject {
//toString()方法
public static void test1() {
Student stu = new Student(20);
String str = new String("Hello");
System.out.println(stu);
System.out.println(str);
}
//hashCode()方法
public static void test2() {
Student stu1 = new Student(20);
Student stu2 = new Student(20);
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(stu1.hashCode()+","+stu2.hashCode());
System.out.println(str1.hashCode()+","+str2.hashCode());
}
//equals()方法
public static void test3() {
Student stu1 = new Student(20);
Student stu2 = new Student(20);
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(stu1.equals(stu2));
System.out.println(str1.equals(str2));
}
public static void main(String[] args) {
test3();
}
}