volatile
and transient
keywords
1) transient
keyword is used along with instance variables to exclude them from serialization process. If a field is transient
its value will not be persisted.
On the other hand, volatile
keyword is used to mark a Java variable as "being stored in main memory".
Every read of a volatile
variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile
variable will be written to main memory, and not just to the CPU cache.
2) transient
keyword cannot be used along with static
keyword but volatile
can be used along with static
.
3) transient
variables are initialized with default value during de-serialization and there assignment or restoration of value has to be handled by application code.
For more information, see my blog:
http://javaexplorer03.blogspot.in/2015/07/difference-between-volatile-and.html