不应该是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/