Understanding the options for overriding the hashCode() and equals(Object obj) methods, I wanted to see what I was redefining, that is, to look at the body of the methods, and when I got into the Object class I was faced with the fact that there were only signatures. Where are the bodies gone? Where can I look at them? Help me find ...

ps detective almost)))

  • Most likely you are viewing the stubs supplied with the IDE, which have no relation to the actual implementation. The signature, meanwhile, is clearly not what you are describing, because the arguments are part of the signature, and they are different in the two methods. - etki

2 answers 2

For the equals method there is an implementation in java

 public boolean equals(Object obj) { return (this == obj); } 

The hashCode method is declared as native , i.e. its implementation is written in c ++. In general, there are several implementations in oracle jvm for this function. To select a specific one, you need to set the flag at the start of jvm

 -XX:hashCode=n 

where n can be:

0 - Park-Miller RNG (default)
1 - f (address, global_statement)
2 - constant 1
3 - Serial counter
4 - Object address
5 - Thread-local Xorshift

It is seen that the default random number generator is set.
See how it is implemented in c ++ by reference.

    The following methods are located in Android/sdk/sources/android-X/java/lang and to Java/jdkX.X.X_X/src.zip;java/lang/ do not have a direct relationship ( thanks @ Regent `y )

    hashCode() in the Object class

     public int hashCode() { int lockWord = shadow$_monitor_; final int lockWordStateMask = 0xC0000000; // Top 2 bits. final int lockWordStateHash = 0x80000000; // Top 2 bits are value 2 (kStateHash). final int lockWordHashMask = 0x0FFFFFFF; // Low 28 bits. if ((lockWord & lockWordStateMask) == lockWordStateHash) { return lockWord & lockWordHashMask; } return System.identityHashCode(this); } 

    equals(Object obj) in class Object

     public boolean equals(Object obj) { return (this == obj); } 
    • 2
      And in my Object class I just have a public native int hashCode (); on the 100th line. Where did you find it? - Pavel
    • @Pavel : in Android Studio, F4 with focus on Object - TimurVI
    • one
      It should be noted that this file is located in the Android / sdk / sources / android-X / java / lang folder, and not in Java / jdkX.X.X_X / src.zip; java / lang / , so you can say that Android your Object , with preference and mademoiselles. - Regent
    • @Regent: thanks a lot, I'll know - TimurVI