Is String s = “foobar” atomic?

后端 未结 2 428
天涯浪人
天涯浪人 2021-01-01 19:54

Is String s = \"foobar\"; atomic?

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

Thanks.

相关标签:
2条回答
  • 2021-01-01 20:44

    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

    0 讨论(0)
  • 2021-01-01 20:45

    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;
    
    0 讨论(0)
提交回复
热议问题