Class relations: + java.lang.Object


Java object class is the root of the class hierarchy. Every class has the object class as a superclass. All objects, including arrays, implement the methods of this class.

MethodDescriptionRef
String toString()It provides string representation of an object
native int hashCode()Generates unique hash code for distinct object
boolean equals(Object obj)It compares object for equality. It is generally necessary to override thehashCode() method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes
final native Class<?> getClass()It gets the actual runtime class of this object
protected Object clone()It returns a new object that is a field-for-field copy of the original object

native: Implemented in a language like C or C++ rather than in Java. This is because Java itself does not provide direct access to the memory addresses of objects.

toString()

In Java, when you print an object, the Object toString() method is called implicitly by default. The method returns a string consisting the name of the class which the object is an instance, followed by an at-sign ”@”, and the hexadecimal representation of the hash code of the object.

Important

There is a common misconception that the hash string returned by the toString() method is the memory address of the object. This is not correct. Neither the hexadecimal representation nor the hash code of the object is the memory address of the object. The hash code is a value generated by the hashCode() method, which serves as a unique identifier (hash value) used by hash-based data structures like HashSet and HashMap for efficient storage and retrieval of objects.

public String toString() {  
    return getClass().getName() + "@" + Integer.toHexString(hashCode());  
}

Example use case

public static void main(String[] args) {
	Object obj = new Object();
	System.out.println(obj);
}
java.lang.Object@8efb846

The 8efb846 does not relate to the object’s memory location, but a unique identifier for the object.

clone()

The clone() method returns a new object that is a field-for-field copy of the original object. The class of the new object must implement the Cloneable interface to allow the clone() method to be called without throwing a CloneNotSupportedException. The clone() method implements the prototype design pattern.

Note

The default implementation of Object clone() method returns a shallow copy. Fields in primitive types are achieved deep copy, for fields that are references to objects, only the references are copied, not the objects themselves.

To create a deep copy, you must override the clone() method to also clone objects reference fields.

class Point implements Cloneable {
    private int x;
    private int y;
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    @Override
    public String toString() {
        return "Point{x=" + x + ", y=" + y + '}';
    }
}
class Line implements Cloneable {
    private Point start;
    private Point end;
    
    public Line(Point start, Point end) {
        this.start = start;
        this.end = end;
    }
    
    // create deep copy
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Line cloned = (Line) super.clone();
        cloned.start = (Point) start.clone();
        cloned.end = (Point) end.clone();
        return cloned;
    }
    
    @Override
    public String toString() {
        return "Line{start=" + start + ", end=" + end + '}';
    }
    public static void main(String[] args) {
        try {
            Point p1 = new Point(1, 2);
            Point p2 = new Point(3, 4);
            Line line1 = new Line(p1, p2);
            Line line2 = (Line) line1.clone();
            System.out.println(line1); 
            System.out.println(line2); 
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
Line{start=Point{x=1, y=2}, end=Point{x=3, y=4}}
Line{start=Point{x=1, y=2}, end=Point{x=3, y=4}}

Back to parent page: Java Standard Edition (Java SE) and Java Programming

Web_and_App_DevelopmentProgramming_LanguagesJavaObject_ClassCloneable

Reference: