Why do java inline functions still have code?

There are many methods in the Java API that are internal, but they have code associated with them when viewing the source code.

For example, Integer.bitCount () is internal, but if you open the file of the Integer class, you can see its code.

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

+7
java intrinsics
source share
2 answers

According to the wiki, the definition of an Intrinsic Function is as follows:

In compiler theory, an internal function is a function available for use in a given programming language, the implementation of which is specially processed by the compiler. Typically, it replaces a sequence of automatically generated instructions to call a function, similar to a built-in function. However, unlike a built-in function, the compiler has an intimate knowledge of the internal function and may therefore better integrate it and optimize it for the situation. This is also called a built-in function in many languages.

He goes on to say, important and relevant to your question:

Compilers that implement internal functions usually only allow them when the user has requested optimization, returning to the default implementation provided otherwise by the language runtime.

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

Also, check out this discussion , which perfectly explains the example using the sample code.

+10
source share

The default implementation is provided if it is not possible to use the internal version, that is, work on a platform for which the embedded version is not provided.

+5
source share

All Articles