gpt4 book ai didi

java - Java Janguage 规范的澄清

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:51 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com