类文件格式的最终​​变量 [英] Final variables in class file format

查看:94
本文介绍了类文件格式的最终​​变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件格式是否支持 final 关键字,以便将其与变量一起使用?

或者它只是推断出有效的最终结果来自代码的变量和JIT编译器基于它执行优化?

Does class file format provide support for final keyword in case of using it with variables?
Or does it just deduce the effective finality of a variable from code and JIT compiler perform optimization based on it?

这里,在类文件格式文档中,他们提到了关于final关键字,但仅限于使用 final block 的情况和最终类

没有关于最终变量的信息。

Here, in class file format documentation, they mentioned about final keyword, but only in case of using it with final block and final class.
There is nothing about final variables.

推荐答案

不,类文件中没有这样的信息编码。

No, there is no such information encoded in class file.

您可以通过编译<$ c $的源文件轻松验证这一点。 c> final 本地变量且没有 final - 结果类将是相同的。

You may easily verify this by compiling a source file with a final local variable and without final - the result classes will be identical.

但是,Java 8添加了 MethodParameters 属性,用于记录有关方法参数的名称和访问标志的信息。这意味着,您可以检查方法参数是否为 final

However, Java 8 added MethodParameters attribute that records information about names and access flags of method arguments. This means, you can check whether a method argument is final or not.

即时编译器可以不需要了解 final 本地人 - 他们可以轻松确定任何表达式的实际范围。即使变量不是最终变量,例如

Just-in-time compilers do not need to know about final locals - they can easily determine the actual scope of any expression. Even if a variable is not final, e.g.

    int x = 1;
    // ... code A ...

    x = 2;
    // ... code B ...

编译器将优化代码 A 好像 x 总是 1 ,代码 B 好像 x 总是 2

the compilers will optimize the code A as if x is always 1, and the code B as if x is always 2.

这篇关于类文件格式的最终​​变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆