Usage of Java's Core Object Class and Objects Utility Class
Object Class
toString() and equals() Methods
The Object class is the top-level parent class of all Java classes, so all objects inherit its default method implementations.
public class ObjectBasicDemo {
public static void main(String[] args) {
Learner learnerA = new Learner("Li Ming", 20);
// Printing an object directly implicitly invokes its toString() method
System.out.println(learnerA.toString());
System.out.println(learnerA);
Learner learnerB = new Learner("Li Ming", 20);
// Compare internal field values instead of memory addresses
System.out.println(learnerB.equals(learnerA));
}
}
class Learner {
private String fullName;
private int userAge;
public Learner(String fullName, int userAge) {
this.fullName = fullName;
this.userAge = userAge;
}
// Getters and setters are omitted for brevity
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Learner target = (Learner) o;
return userAge == target.userAge && Objects.equals(fullName, target.fullName);
}
@Override
public String toString() {
return "Learner{" +
"fullName='" + fullName + '\'' +
", userAge=" + userAge +
'}';
}
}
The default toString() implementation returns the fully qualified class name plus the hexadecimal representation of the object's hash code. Override this method to return readable object state information.
The default equals() implementation compares weather two object references point to the same memory address. Override this method to define custom equality logic based on internal field values.
clone() Method for Object Duplication
Shallow Cloning
Shallow cloning copies all field values directly, including memory addresses for reference-type fields. The original and cloned objects will share the same instance for all reference-type fields.
// Implement Cloneable marker interface to enable cloning capability
public class Learner implements Cloneable {
private String fullName;
private int userAge;
private double[] gradeRecords;
public Learner() {}
public Learner(String fullName, int userAge, double[] gradeRecords) {
this.fullName = fullName;
this.userAge = userAge;
this.gradeRecords = gradeRecords;
}
// Getters and setters are omitted for brevity
@Override
protected Object clone() throws CloneNotSupportedException {
// Shallow clone uses default parent class implementation
return super.clone();
}
}
public class ShallowCloneDemo {
public static void main(String[] args) throws CloneNotSupportedException {
Learner original = new Learner("Li Ming", 20, new double[]{91.5, 87, 93});
Learner cloned = (Learner) original.clone();
System.out.println(original.getFullName());
System.out.println(original.getUserAge());
System.out.println(original.getGradeRecords());
System.out.println(cloned.getFullName());
System.out.println(cloned.getUserAge());
// Output address is identical to original, confirming shared reference
System.out.println(cloned.getGradeRecords());
}
}
Deep Cloning
Deep cloning creates independent copies for all reference-type fields, so modifications to the cloned object's reference fields will not affect the original object.
Modify the clone() method implementation as follows to achieve deep cloning:
@Override
protected Object clone() throws CloneNotSupportedException {
Learner clonedInstance = (Learner) super.clone();
// Create independent copy of the reference-type gradeRecords array
clonedInstance.gradeRecords = clonedInstance.gradeRecords.clone();
return clonedInstance;
}
Objects Utility Class
The java.util.Objects class provides null-safe utility methods for common object operations. Its equals() method avoids NullPointerExceptions that ocurr when calling equals() on a null object.
public class ObjectsUtilDemo {
public static void main(String[] args) {
Learner learnerA = null;
Learner learnerB = new Learner("Li Ming", 20);
// The line below will throw NullPointerException if uncommented
// System.out.println(learnerA.equals(learnerB));
// Objects.equals handles null inputs safely
System.out.println(Objects.equals(learnerA, learnerB));
}
}
Objects.equals(a, b) returns true if both parameters are null, false if exactly one parameter is null, and calls a.equals(b) for non-null inputs.