Java中静态和非静态最终原始字段之间的性能差异 [英] Performance differences between static and non-static final primitive fields in Java

查看:166
本文介绍了Java中静态和非静态最终原始字段之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

private final int period = 1000;

在这种特殊情况下,作者原本打算让它也是静态的,因为价值不能在任何时候都没有改变,没有真正的功能性原因不要声明它是静态的,但它让我想知道Java如何处理最终的静态原语和最终的静态原语。

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.

特别是:

1)最终的静态基元是如何存储的?它们是否只是直接编译到它们被使用的表达式中?

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

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

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)编译器现在是否足够聪明,以确定在上述情况下,变量是有效静态的,因为不可能让不同的实例包含不同的值,因此优化它与最终的static one?

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?

推荐答案


1)最终的静态基元是如何存储的?它们是否只是直接编译到它们被使用的表达式中?

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

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

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


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

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)编译器现在是否足够聪明,以确定在上述情况下,变量是有效静态的,因为不可能让不同的实例包含不同的值,从而优化它类似于最终的静态吗?

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?

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

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)

这篇关于Java中静态和非静态最终原始字段之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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