Monday, 17 June 2019

Core Java | Difference between volatile and transient keyword

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.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...