Java 中静态和非静态最终基元字段之间的性能差异

发布于 2024-09-26 06:49:35 字数 428 浏览 7 评论 0原文

我最近遇到一个声明了以下字段的类:

private final int period = 1000;

在这种特殊情况下,作者打算将其设置为静态,并且由于该值在任何时候都无法更改,因此没有真正的功能原因不声明它是静态的,但它让我想知道Java如何对待final和final静态原语。

特别是:

1)最终静态原语如何存储?它们只是直接编译到使用它们的表达式中吗?

2)如果它们实际上被分配了存储,那么包含类的每个实例是否都必须维护对该位置的引用? (在这种情况下,对于小于 4 字节的原语,该类的每个实例实际上会比直接包含原语(就像在非静态情况下那样)更大)

3) 编译器现在是否足够聪明,可以确定在上述情况下,变量是“有效静态”的,因为不可能让不同的实例包含不同的值,因此与最终静态变量类似地对其进行优化?

I recently ran across a class that had the following field declared:

private final int period = 1000;

In this particular case, the author had intended for it to also be static and since the value couldn't be altered at any point, there was no real functional reason not to declare it static, but it got me wondering how Java treats final vs. final static primitives.

In particular:

1) How are final static primitives stored? Are they simply compiled directly into the expressions in which they're used?

2) If they are actually allocated storage, does each instance of the containing class then have to maintain a reference to that location? (in which case, for primitives of less than 4 bytes, each instance of the class would actually be larger than if it simply included the primitive directly as it would in the non-static case)

3) Are compilers now smart enough to determine that in cases such as the one above, the variable is 'effectively static' since it would be impossible to have different instances contain different values and therefore optimize it similarly to a final static one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-10-03 06:49:35

1) 最终静态原语如何存储?它们只是直接编译到使用它们的表达式中吗?

不直接编译到表达式中。它们被编译到 .class 文件中并由操作码 ldc 引用。

2) 如果它们实际上分配了存储空间,那么包含类的每个实例是否都必须维护对该位置的引用? (在这种情况下,对于小于 4 字节的基元,类的每个实例实际上会比直接包含基元(就像在非静态情况下那样)更大)

不,“引用”被烘焙到字节码,因此不需要在每个实例的基础上存储任何内容。

3) 编译器现在是否足够聪明,可以确定在上述情况下,变量是“有效静态”的,因为不同的实例不可能包含不同的值,因此与最终静态变量类似地对其进行优化?

不确定,但我怀疑它是否在编译器级别进行了优化。 JIT 可能会发挥作用。但是,我完全不确定您期望什么样的“性能差异”。无论哪种情况,对性能的影响都可以忽略不计。 (静态/非静态/最终/非最终)

1) How are final static primitives stored? Are they simply compiled directly into the expressions in which they're used?

Not compiled directly into the expressions. They are compiled into the .class file and referenced by the opcode ldc.

2) If they are actually allocated storage, does each instance of the containing class then have to maintain a reference to that location? (in which case, for primitives of less than 4 bytes, each instance of the class would actually be larger than if it simply included the primitive directly as it would in the non-static case)

No, the "reference" is baked into the bytecode, so nothing needs to be stored on a per-instance basis.

3) Are compilers now smart enough to determine that in cases such as the one above, the variable is 'effectively static' since it would be impossible to have different instances contain different values and therefore optimize it similarly to a final static one?

Not sure, but I doubt it's optimized at the compiler level. The JIT would probably come into play. However, I'm not at all sure what sort of "performance differences" you are expecting. No matter what the case, the performance impact will be negligible. (static/non-static/final/non-final)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文