Java API中有许多内在函数的方法,但在查看源代码时仍然有与它们相关的代码。
There are many methods in the Java API that are intrinsics, but still have code associated with them when looking at the source code.
例如,Integer。 bitCount()是一个内在的,但是如果你打开Integer类文件,就可以看到它的代码。
For an example, Integer.bitCount() is an intrinsic, but if you open the Integer class file, you can see code with it.
如果不一定使用这个代码,它的用途是什么?由编译器/ jvm?
What purposes might this code serve if it is not necessarily used by the compiler/jvm?
推荐答案根据 wiki , intrinsic 的定义函数如下:
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.
因此,这意味着在优化之前大部分时间都会使用默认实现未请求或不可能(这取决于运行JVM的计算机/配置)。 JVM可以将整个 Integer.bitCount()代码替换为优化的机器代码指令。
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.
更多推荐
为什么java内部函数仍然有代码?
发布评论