为什么java内部函数仍然有代码?

编程入门 行业动态 更新时间:2024-10-27 09:32:33
本文介绍了为什么java内部函数仍然有代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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内部函数仍然有代码?

本文发布于:2023-11-25 16:38:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   代码   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!