The explanation of hashCode() method and toString() method of Object class in Java:-
=====================================================================
hashcode() method:-
===================
public native int hashCode()
{
return ||It will return an integer value||;
}
The hashcode() is a predefined method that is placed in the predefined class by the name of Object. When you invoke this method, it will return an unique integer value.
For Example:-
===========
A obj=new A();
System.out.println(obj.hashCode());//366712642
When you call the hashcode method on top of the reference variable of an Object, the hashCode() method of the Object class will be invoked and which is going to return an unique integer value.
toString() method:-
=================
public String toString()
{
return ||ClassName@HedecimalValueofgeneratedhashcode||;
}
The implementation of the 'toString()' method is already given in the Object class and this is also a predefined method in the object class. When you call this 'toString()' method, it is going to return a String value which consists of the class name followed by the "@" symbol and the unsigned represention of the hexadecimal number of the hashcode.
Example :-
=========
A obj=new A();
System.out.println(obj);
or
System.out.println(obj.toString());//A@15db9742
Whenever, an object is created, the JVM is going to generate an hashcode and internally toString method will be called which depends upon this hashcode and a unique integer reference value will be generated and will be assigned to the reference variable which indeed gets stored in the stack area. And, the objects are usually gets created in the heap area with the reference values.
Object Class:-
===========
Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of Object and if extends another class then it is indirectly derived. Therefore the Object class methods are available to all Java classes.
Whenever, the user defined class is not extending to any other class, compiler going to consider the Object class as the Parent class.
Please be informed that there are 11 methods available in the Object class. These methods are commonly used by all the classes.
class A extends Object
{
int method1()
{
return 100;
}
public static void main(String[] args)
{
A obj1=new A();
A obj2=new A();
A obj3=new A();
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
}
}
output:-
======
A@15db9742
A@6d06d69c
A@7852e922