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.
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 thehashCode()
method, which serves as a unique identifier (hash value) used by hash-based data structures likeHashSet
andHashMap
for efficient storage and retrieval of objects.
Example use case
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.
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: