Let the scenario be the following:
I have a volatile String str
that is shared between multiple threads. As String
is immutable, while we can change the reference str
, any Object it is pointing to at any time is read-only.
So now, let's say I have a method in one of my threads that reads in str
.
Given the reading in of str
is not synchronized, is it guaranteed that the Object the method reads in as str
stays the same, even if at some time during execution the reference str
is overwritten?
I.e., if a method gets passed a reference, does it save a local copy of it (so that changing the original reference afterwards has no effect on the method)?
Is this statement true for any general immutable object/primitive?