java - 删除 final 关键字如何改变程序的行为方式?

标签 java final

这个问题主要不是关于字符串。出于学术好奇,我想知道变量上的 final 修饰符如何改变程序的行为。以下示例表明这是可能的。

这些行打印true

final String x = "x";
System.out.println(x + x == "xx");

但是这些行打印false

String x = "x";
System.out.println(x + x == "xx");

除了 String 实习之外,如果从变量声明中删除修饰符 final,是否还有任何其他因素会导致程序的行为发生变化?我假设程序编译时使用或不使用修饰符。

请不要投票将其作为 Comparing strings with == which are declared final in Java 的副本关闭.我了解 String 的示例。

我想问是否有任何其他原因删除 final 修饰符会有所不同。请有人链接到答案或回答问题。谢谢。

最佳答案

final 修饰符只保证变量是definitely assigned ,并禁止对该变量进行任何重新分配。

唯一可以观察到的特殊情况是 expressly stated in the JLS :

A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable.

Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).

有相当数量的 JLS 阅读,涵盖要点:作者 JLS §13.4.9 ,您不会移除 final 修饰符时遇到任何不良影响。

但是, by JLS 17.5 ,如果您依赖于线程只看到它可以观察到的对象中明确分配的变量的保证,那么删除 final 变量将导致这些变量不再可见另一个线程。


所以,如果我们看一下 class initialization首先,如果字段是静态的并且不是常量变量,则有围绕类初始化的规则:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).

JLS §13.1 , 明确指出将字段更改为 final 会破坏 binary compatibility :

References to fields that are constant variables (§4.12.4) are resolved at compile time to the constant value that is denoted. No reference to such a field should be present in the code in a binary file (except in the class or interface containing the field, which will have code to initialize it). Such a field must always appear to have been initialized (§12.4.2); the default initial value for the type of such a field must never be observed. See §13.4.9 for a discussion.

从 13.4.9 开始:

If a field that was not declared final is changed to be declared final, then it can break compatibility with pre-existing binaries that attempt to assign new values to the field.

Deleting the keyword final or changing the value to which a field is initialized does not break compatibility with existing binaries.

If a field is a constant variable (§4.12.4), then deleting the keyword final or changing its value will not break compatibility with pre-existing binaries by causing them not to run, but they will not see any new value for the usage of the field unless they are recompiled. This is true even if the usage itself is not a compile-time constant expression (§15.28).

This result is a side-effect of the decision to support conditional compilation, as discussed at the end of §14.21.

因此,仅从这一点来看,要小心突然将字段更改为 final删除字段是安全的

...但这只适用于单线程世界。来自 JLS 17.5 :

Fields declared final are initialized once, but never changed under normal circumstances. The detailed semantics of final fields are somewhat different from those of normal fields. In particular, compilers have a great deal of freedom to move reads of final fields across synchronization barriers and calls to arbitrary or unknown methods. Correspondingly, compilers are allowed to keep the value of a final field cached in a register and not reload it from memory in situations where a non-final field would have to be reloaded.

final fields also allow programmers to implement thread-safe immutable objects without synchronization. A thread-safe immutable object is seen as immutable by all threads, even if a data race is used to pass references to the immutable object between threads. This can provide safety guarantees against misuse of an immutable class by incorrect or malicious code. final fields must be used correctly to provide a guarantee of immutability.

An object is considered to be completely initialized when its constructor finishes. A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.

因此,如果您的程序依赖于上述保证才能正常运行,那么删除 final 关键字将对线程产生影响。

关于java - 删除 final 关键字如何改变程序的行为方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200148/

相关文章:

java - Eclipse 全天关闭

java - 根据 RFC 3986 的无效 URI 示例

java - 为什么我们需要在 Java 中进行多进程编程?

java - 代码多态性示例之间的区别

Java Switch语句困惑

java - "Once-initialized"构造时未初始化的变量

java - 将属性更改为最终属性

java - java中的不可变函数参数

java - 通过引用递归传递对象? java

java - 创建不可变类的最佳设计方法