Why do java intrinsic functions still have code?

Tags: ,



There are many methods in the Java API that are intrinsics, but still have code associated with them when looking at the source code.

For an example, Integer.bitCount() is an intrinsic, but if you open the Integer class file, you can see code with it.

What purposes might this code serve if it is not necessarily used by the compiler/jvm?

Answer

As per wiki, the definition of Intrinsic Function is as follows:

In compiler theory, an intrinsic function is a function available for
use in a given programming language whose implementation is handled
specially by the compiler. Typically, it substitutes a sequence of
automatically generated instructions for the original function call,
similar to an inline function. Unlike an inline function though, the
compiler has an intimate knowledge of the intrinsic function and can
therefore better integrate it and optimize it for the situation. This
is also called builtin function in many languages.

Further it says, important and relevant to your question:

Compilers that implement intrinsic functions generally enable them
only when the user has requested optimization, falling back to a
default implementation provided by the language runtime environment
otherwise.

So, it means a default implementation is used most of the time until optimization is not requested or possible (this depends on which machine/configuration JVM is running on). JVM can replace the whole Integer.bitCount() code to an optimized machine code instruction.

Further, check this discussion which explains the point nicely with an example code.



Source: stackoverflow