String s = "foobar" 是吗?原子?

发布于 2024-12-04 08:54:07 字数 85 浏览 1 评论 0 原文

String s = "foobar"; 是原子的吗?

分配对象引用应该是,但我不太确定。

谢谢。

Is String s = "foobar"; atomic?

Assigning a object-reference should be, but I'm no really sure.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟燃烟灭 2024-12-11 08:54:07

。在java中所有的引用赋值都是原子的。

请注意,像 String s = new String("foobar") 这样的复合语句不是原子的,因为它包含对象创建和单独的赋值。

另请注意,“对 long 和 double 变量的赋值可能不是原子的”,来自 JLS-17.7

Yes. All reference assignments are atomic in java.

Just note that a composite statement like String s = new String("foobar") is not atomic, because it comprises of an object creation and then an assignment separately.

Also note that "assignments to long and double variables may not be atomic", from JLS-17.7

掩于岁月 2024-12-11 08:54:07

这里已经给出了许多很好的答案。我仍然想要一些更“官方”的关于“所有引用分配在 java 中都是原子的”这样的声明,以及为什么 String s = "foobar" 不在运行时创建新对象。以下是 Java 语言规范 中的内容。 (缩写JLS)。

下面是一些示例:

String str1 = "foo";                // line 1, atomic
String str2 = "foo" + "bar";        // line 2, atomic
String str3 = str1;                 // line 3, atomic
String str4 = str1 + str2;          // line 4, not atomic
String str5 = new String("foobar"); // line 5, not atomic

第 1 行和第 2 行是原子的,因为:

  1. 它们都是常量表达式,并且在编译时计算。运行时没有发生对象构造。

JLS - 15.28:常量表达式是表示基本类型值或不会突然完成的字符串的表达式,并且由...基本类型文字和字符串类型文字组成。

JSL - 3.10.5:由常量表达式(第 15.28 节)计算的字符串在编译时计算,然后将其视为文字。

  1. 写入和读取引用始终是原子的。

JLS - 17.7:引用的写入和读取始终是原子的,无论它们是实现为 32 位还是 64 位值。

第 3 行是原子的,因为:

该行中只有一个引用赋值。

JLS - 17.7

第 4 行不是原子,因为:

通过连接两个字符串变量,在运行时创建一个新的 String 对象。对象构造不是原子的。

JLS - 15.8.1 - 字符串连接运算符 +:除非表达式是常量表达式(第 15.28 节),否则新创建字符串对象(第 12.5 节)。

JLS - 3.10.5 - 字符串文字:在运行时通过连接计算的字符串是新创建的,因此是不同的。

第 5 行不是原子的,因为:

String 对象是在运行时在该行中构造的。

Many great answers have already been give here. Still I want something more "official" about claims like "All reference assignments are atomic in java", and why String s = "foobar" does not create a new object at runtime. Here is what is written in Java Language Spec. (Abbr. JLS).

Below are some examples:

String str1 = "foo";                // line 1, atomic
String str2 = "foo" + "bar";        // line 2, atomic
String str3 = str1;                 // line 3, atomic
String str4 = str1 + str2;          // line 4, not atomic
String str5 = new String("foobar"); // line 5, not atomic

Line 1 and line 2 are atomic because:

  1. They are both constant expressions, and are computed at compile time. There are no object construction occurred at runtime.

JLS - 15.28: A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using ... literals of primitive type and literals of type String.

JSL - 3.10.5: Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

  1. Writes to and reads of references are always atomic.

JLS - 17.7: Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values.

Line 3 is atomic because:

There is only a reference assignment in this line.

JLS - 17.7

Line 4 is not atomic because:

By concat two string variables, a new String object is created at runtime. Object construction is not atomic.

JLS - 15.8.1 - String Concatenation Operator +: The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).

JLS - 3.10.5 - String literals: Strings computed by concatenation at run time are newly created and therefore distinct.

Line 5 is not atomic because:

A String object is constructed at runtime in this line.

懵少女 2024-12-11 08:54:07

是的,但如果您担心竞争条件,您至少应该注意“同步”方法/块。

请注意,这不是原子的,因为它包含两个操作:

String s = string_a + string_b;

Yes, but if you're worried about race conditions, you should at least be aware of 'synchronized' methods/blocks.

And note that this is not atomic because it contains two operations:

String s = string_a + string_b;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文