What are the Object class methods

Heeah
2 min readDec 30, 2023

--

Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class.

toString()

The toString() provides a String representation of an object and is used to convert an object to a String.

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

It is always recommended to override the toString() method to get our own String representation of Object

hashCode()

For every object, JVM generates a unique number which is a hashcode. It returns distinct integers for distinct objects. A common misconception about this method is that the hashCode() method returns the address of the object, which is not correct. It converts the internal address of the object to an integer by using an algorithm. The hashCode() method is native because in Java it is impossible to find the address of an object, so it uses native languages like C/C++ to find the address of the object.

It returns a hash value that is used to search objects in a collection. JVM(Java Virtual Machine) uses the hashcode method while saving objects into hashing-related data structures like HashSet, HashMap, Hashtable, etc. The main advantage of saving objects based on hash code is that searching becomes easy.

Reference
https://www.geeksforgeeks.org/object-class-in-java/
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html

--

--