Difference between transient vs volatile variable in Java

Minh Pham
An Idea (by Ingenious Piece)
4 min readJan 31, 2020

Transient

What

Transient keyword is used in the serialization process to prevent any variable from being serialized.

Why

Provides you some control over the serialization process and gives you the flexibility to exclude some of the object properties from the serialization process

When

  • Transient keyword can only be applied to fields or member variables. Applying it to the method or local variable is a compilation error.
  • Another important point is that you can declare a variable static and transient at the same time and java compiler doesn’t complain but doing that doesn’t make any sense because transient is to instruct “do not save this field” and static variables are not saved anyway during serialization.
  • In a similar way you can apply transient and final keyword together to a variable compiler will not complain but you will face another problem of reinitializing a final variable during deserialization.
  • Transient variable in java is not persisted or saved when an object gets serialized.

Volatile

What

The volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache the value of this variable and always read it from main memory.

From Java 5 along with major changes like Autoboxing, Enum, Generics and Variable arguments, Java introduces some change in Java Memory Model (JMM), Which guarantees visibility of changes made from one thread to another also as “happens-before” which solves the problem of memory writes that happen in one thread can “leak through” and be seen by another thread.

How

  • Cannot be used with method or class and it can only be used with a variable.
  • Volatile keyword also guarantees visibility and ordering, after Java 5 write to any volatile variable happens before any read into the volatile variable
  • The use of volatile keyword also prevents compiler or JVM from the reordering of code or moving away from the synchronization barrier.

When

Important points on Volatile keyword in Java

  • The volatile keyword in Java is the only applied to a variable and using a volatile keyword with class and method is illegal.
  • volatile keyword in Java guarantees that the value of the volatile variable will always be read from the main memory and not from Thread’s local cache.

Since volatile keyword in Java only synchronizes the value of one variable between Thread memory and "main" memory while synchronized synchronizes the value of all variable between thread memory and "main" memory and locks and releases a monitor to boot. Due to this reason synchronized keyword in Java is likely to have more overhead than volatile.

  • In Java reads and writes are atomic for all variables declared using Java volatile keyword (including long and double variables).
  • Using the volatile keyword in Java on variables reduces the risk of memory consistency errors because any write to a volatile variable in Java establishes a happens-before relationship with subsequent reads of that same variable.
  • From Java 5 changes to a volatile variable are always visible to other threads. It means that when a thread reads a volatile variable in Java, it sees not just the latest change to the volatile variable but also the side effects of the code that led up the change.

Also from Java 5 writing into a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire

  • Reads and writes are atomic for reference variables are for most primitive variables (all types except long and double) even without the use of volatile keyword in Java.
  • Access to a volatile variable in Java never has a chance to block, since we are only doing a simple read or write, so unlike a synchronized block we will never hold on to any lock or wait for any lock.
  • You can not synchronize on the null object but your volatile variable in Java could be null.
  • Java volatile keyword doesn’t mean atomic, its common misconception that after declaring volatile ++ will be atomic, to make the operation atomic you still need to ensure exclusive access using synchronized method or block in Java.

Synchronized obtains and releases the lock on monitor’s Java volatile keyword doesn’t require that => Synchronized method affects performance more than a volatile keyword in Java.

  • If a variable is not shared between multiple threads, you don’t need to use volatile keyword with that variable.
  • The volatile keyword in Java is a field modifier while synchronized modifies code blocks and methods.

Difference between transient vs volatile variable or modifier in Java

  • transient keyword is used along with instance variables to exclude them from the serialization process. volatile keyword can also be used in variables to indicate compiler and JVM that always read its value from main memory and follow happens-before relationship on the visibility of volatile variable among multiple threads
  • transient keyword can not be used along with static keyword but volatile can be used along with static.
  • transient variables are initialized with default value during de-serialization and there assignment or restoration of value has to be handled by application code.

--

--