Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is String s = "foobar" atomic?

Tags:

java

atomic

Is String s = "foobar"; atomic?

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

Thanks.

like image 764
melbic Avatar asked Sep 14 '11 18:09

melbic


People also ask

Is string an atomic?

Atomic Data Types XML schema has no concept of a character as a data type and a string is an atomic primitive type.

Is string a variable or object?

Strings are Reference/Complex Object Type Strings in Java fall under the category of Reference/Complex Object data types. They store a reference to the object rather than the object itself. When you assign a string variable to another variable, It actually copies the reference to the object but not the object itself.


1 Answers

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

like image 114
Suraj Chandran Avatar answered Oct 05 '22 19:10

Suraj Chandran