Difference between volatile and transient in java



In this article, we will find the differences between volatile and Transient. Both have a different purpose for specifying before a variable. These modifiers are important in determining the behavior and characteristics of variables in different cases.

What is volatile?

A volatile keyword is used in a multithreading environment where two threads reading and writing the same variable simultaneously. The volatile keyword flushes the changes directly to the main memory instead of the CPU cache. 

Example of Volatile

The following is an example of Volatile in Java:

public class VolatileExmaple extends Thread {
   volatile boolean isRunning = true;

   public void run() {
      long count = 0;
      while (isRunning) {
         count++;
      }
      System.out.println("Thread terminated. " + count);
   }

   public static void main(String[] args) throws InterruptedException {
      VolatileExmaple t = new VolatileExmaple();
      t.start();
      Thread.sleep(2000);
      t.isRunning = false;
      t.join();
      System.out.println("isRunning set to " + t.isRunning);
   }
}

The output of the above Java program is:

Thread terminated. 4562891748
isRunning set to false

What is transient?

The transient keyword is used during serialization. Fields that are marked as transient can not be part of the serialization and deserialization. We don't want to save the value of any variable then we use transient keyword with that variable. 

Example of Transient

The following is an example of Transient in Java:

import java.io.Serializable;

public class TransientExample implements Serializable {
   transient int age;           // will not be serialized
   private String name;
   private String address;

   public TransientExample(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }

   public void display() {
      System.out.println("Name: " + name);
      System.out.println("Address: " + address);
      System.out.println("Age: " + age);
   }

   public static void main(String[] args) {
      TransientExample obj = new TransientExample("John", "New York", 25);
      obj.display();
   }
}

The output of the above Java program is:

Name: John
Address: New York
Age: 25

Difference between Volatile and Transient

The following table shows the difference between volatile and transient:

Sr. No. Key Volatile Transient
1
Basic 
Volatile keyword is used to flush changes directly to the main memory
The transient keyword is used to exclude variable during serialization 
2.
Default value 
Volatile are not initialized with a default value.
During deserialization, transient  variables are initialized with a default value 
3
Static 
Volatile can be used with a static variable.
Transient can not be used with the static keyword
4
Final 
Volatile can be used with the final keyword
Transient can not be used with the final keyword
Teja Kolloju
Teja Kolloju

Code talks. I translate.

Updated on: 2025-04-17T18:59:16+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements