41

I checked the source code of Object class where I found that method declaration of getClass() was

public final native Class<?> getClass();

And the declaration of hashCode() was

public native int hashCode();

Why are these two methods native methods in the class and how can I get the source code of those methods?

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • 15
    not a duplicate - the OP knows what native is, but wants to know _why_ these two methods specifically are. – Alnitak May 14 '12 at 07:03
  • 5
    hashCode() is native because the way in which data is stored can differ on unalike operating systems. I'm not sure why getClass() is though; possibly due to differing implementations of polymorphism. – FThompson May 14 '12 at 07:04
  • 1
    @Vulcan getClass() is final so you can't override it and break the type system. – user207421 May 14 '12 at 08:31
  • @EJP I know why it's final, but not why it's native. As I said in my last comment, my only guess would be due to differing implementations of polymorphism. – FThompson May 14 '12 at 17:49
  • 1
    Well, `hashCode` could be implemented with `System.identityHashCode`. – zch Dec 18 '12 at 22:44

3 Answers3

51

You can find the complete source code of the native methods here

I hope this will work for you.

These are native methods, because it has to interact with the machine. Here machine dependent code is written in the C language, which is not coming with the source package or in rt.jar of the lib location of the Java Runtime Environment (JRE).

One more reason for being native is possibly for the performance reasons. Due to the C level programming performance may be improved, hence they may have written the native code in the C language.

The methods are native because they concern native data. The hashCode method returns an integer value dependent on the internal representation of a pointer to an object on the heap. The getClass method must access the internal vtbl (virtual function table) that represents the compiled program's class hierarchy. Neither of these is possible with core Java.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • 1
    `The hashCode method returns an integer value dependent on the internal representation of a pointer to an object on the heap.` That doesn't seem to be the case from looking at the [source](http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/tip/src/share/vm/runtime/synchronizer.cpp#l555). – Matthias Braun Apr 26 '15 at 18:18
40

Source code for Object class can be found here

This source contains implementation of getClass() method (See line 58). hashCode is defined as a function pointer JVM_IHashCode (See line 43).

JVM_IHashCode is defined in jvm.cpp. See code starting from line 504. This in turn calls ObjectSynchronizer::FastHashCode which is defined in synchronizer.cpp. See implementation of FastHashCode at line 576 and get_next_hash at line 530.

Probably, the methods are native for performance and due to practical issues w.r.t implementation.

For e.g., From javadocs, hashCode is typically implemented "by converting the internal address of the object into an integer". This internal address is not available via java sdk and will have to be implemented as a native method.

Please read Is it possible to find the source for a Java native method?. Also read this blog post Object.hashCode implementation. It gives more details. But makes a wrong assertion that hashCode is not generated from object's identity.

Hope it helps.

Community
  • 1
  • 1
krishnakumarp
  • 8,967
  • 3
  • 49
  • 55
  • Then how can we say that JVM is platform independent ? – Bhavik Ambani May 14 '12 at 08:57
  • How would this break platform independence? For any object, hashCode is not required to be same across platforms. For that matter, it wont even be same on the same platform, across different runs. Try public class TestHashCode { public static void main(String[] args) { Object o = new Object(); System.out.println(o.hashCode()); } } – krishnakumarp May 14 '12 at 16:50
  • 3
    @BhavikAmbani This is terminology nitpicking, but *JVM* is not platform independent, but rather the platform-dependent part, which executes the platform independent Java bytecode on a given platform. – hyde Dec 17 '12 at 13:24
  • @hyde Thats what I am saying, refer my answer. – Bhavik Ambani Dec 17 '12 at 13:32
2

The information for these is in the header (for the class) or elsewhere (for the hashCode) This is not something you can implement in Java. The source for these methods is in the source for the JVM. e.g. you can download the source for OpenJDK.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130