I am quoting from Oracle's Java documentation on Atomic Access
- Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
- Reads and writes are atomic for all variables declared
volatile(including long and double variables).
I understand how volatile works. But mentioning the requirement to declare volatile explicitly for long and double variables to get atomic access in the second statement, is making volatile declaration for reference variables and for most primitive variables (all types except long and double) in the first statement optional.
But I am seeing codes which use explicit volatile declaration in int primitive type to achieve atomic access; and not doing so not guaranteeing atomic access.
int variable1; // no atomic access
volatile int variable2; // atomic access
Am I missing something?