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
}
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.
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()
.
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?
Java hashcode () 1 A hashcode is an integer value associated with every object in Java, facilitating the hashing in hash tables. 2 To get this hashcode value for an object, we can use the hashcode () method in Java. ... 3 Since this method is defined in the Object class, hence it is inherited by user-defined classes also. Weitere Artikel...
FYI: even if a class overrides hashCode (), you can always get the identity hash code of an object o by calling System.identityHashCode (o). Common wisdom is that the identity hash code uses the integer representation of the memory address.
If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same value.
If two objects are equal according to the equals (Object) method, calling the hashCode () method on each of the two objects must produce the same value. If two objects are unequal according to the equals (java.lang.Object) method, calling the hashCode method on each of the two objects doesn't need to produce distinct integer results.
Even though there are some answers here stating that the default implementation is "memory" based, this is plain wrong. This is not the case for a lot of years now.
Under java-8, you can do :
java -XX:+PrintFlagsFinal | grep hashCode
To get the exact algorithm that is used (5
being default).
0 == Lehmer random number generator,
1 == "somehow" based on memory address
2 == always 1
3 == increment counter
4 == memory based again ("somehow")
5 == read below
By default (5
), it is using Marsaglia XOR-Shift algorithm, that has nothing to do with memory.
This is not very hard to prove, if you do:
System.out.println(new Object().hashCode());
multiple times, in a new VM all the time - you will get the same value, so Marsaglia XOR-Shift starts with a seed (always the same, unless some other code does not alter it) and works from that.
But even if you switch to some hashCode that is memory based, and Objects potentially move around (Garbage Collector calls), how do you ensure that the same hashCode is taken after GC has moved this object? Hint: indentityHashCode and Object headers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With