java - Java Janguage 规范的澄清

标签 java jls

不应该是this explanation from the JLS中的myS.equals("/usr")吗? ?

Final fields are designed to allow for necessary security guarantees. Consider the following example. One thread (which we shall refer to as thread 1) executes

Global.s = "/tmp/usr".substring(4);

while another thread (thread 2) executes

String myS = Global.s;
if (myS.equals("/tmp"))System.out.println(myS);

String objects are intended to be immutable and string operations do not perform synchronization.

最佳答案

实际上不是,乍一看你可能会这么想,但那是故意的。进一步引用文本(强调我的):

[...] if the fields of the String class were not final, then it would be possible (although unlikely) that thread 2 could initially see the default value of 0 for the offset of the string object, allowing it to compare as equal to "/tmp"

关于java - Java Janguage 规范的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18894946/

相关文章:

java - JUnit 测试函数的 CodeLens 未出现在 VSCode 中

java - 我可以为在 Java6 Rhino 脚本引擎中运行的 javascript 创建一个 'window' 对象吗

java - 方法签名和方法的删除方式不同?

java - 实现方法抛出异常,但接口(interface)方法未定义,因为它抛出异常

java - 哪些操作可能(不会)抛出 StackOverflowError?

c# - 一种语言将 undefined 评估为等于 false 是否很常见?如果是这样,为什么这样做?

java - 为什么在 Tomcat 下运行的应用程序只使用了一半的可用 CPU?

java - Java 中的子上下文在堆栈中是单独的行吗?

java - 我应该如何阅读 JLS 中的语法描述?

java - 在多个谷歌地图标记之间绘制所有可能的折线?