I was trying to understand the hashCode() for Java's Object and saw the following code for Java Object's hashCode() method:
package java.lang;
public class Object {
// Some more code
public native int hashCode();
// Some other code
}
Now, we know that if we create a class it implicitly extends the Object class, and to do this I wrote a sample example:
package com.example.entity;
public class FirstClass {
private int id;
private String name;
// getters and setters
}
So, this class viz: FirstClass would be extending the Object class implicitly.
Main class:
package com.example.app.main;
import com.example.entity.FirstClass;
public class MainApp {
public static void main(String[] args) {
FirstClass fs = new FirstClass();
fs.setId(1);
fs.setName("TEST");
System.out.println("The hasCode for object fs is " + fs.hashCode());
}
}
As FirstClass is extending Object class implicitly, hence it would have Object classes' hashCode() method.
I invoked the hashCode() on FirstClass object , and as I haven't overridden the hashCode(), by theory it should invoke Object class's hashCode().
My doubt is:
As Object class don't have any implementation, how is it able to calculate the hash-code for any object?
In my case, when I run the program the hash-code which it returned was 366712642.
Can anyone help me understand this?