删除 final 关键字如何改变程序的行为方式?
How can removing final keyword change the way a program behaves?
这个问题主要不是关于字符串的。出于学术好奇心,我想知道变量上的 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
的例子。
我想问是否有任何 other 原因删除 final
修饰符可以有所作为。请有人 link 回答或回答问题。谢谢。
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,如果字段是静态的并且 不是 常量变量,则有围绕 class 初始化的规则:
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, it is spelled out that changing a field to final
can break 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
关键字将对线程产生影响。
这个问题主要不是关于字符串的。出于学术好奇心,我想知道变量上的 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
的例子。
我想问是否有任何 other 原因删除 final
修饰符可以有所作为。请有人 link 回答或回答问题。谢谢。
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,如果字段是静态的并且 不是 常量变量,则有围绕 class 初始化的规则:
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, it is spelled out that changing a field to final
can break 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
关键字将对线程产生影响。