Difference Between Volatile And Transient In Java - Tutorialspoint
Maybe your like
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 falseWhat 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: 25Difference 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 |
Tag » What Is Transient In Java
-
Java Programming/Keywords/transient - Wikibooks, Open Books For ...
-
Transient Keyword In Java - GeeksforGeeks
-
The Transient Keyword In Java - Baeldung
-
Transient In Java | What, Why And How It Works - Edureka
-
Java Transient Keyword - Javatpoint
-
Why Does Java Have Transient Fields? - Stack Overflow
-
What Is The Transient Keyword In Java?
-
Java Transient이란? - Nesoy Blog
-
What Is Transient Variable In Java? Serialization Example - Java67
-
Java Transient Keyword Example - HowToDoInJava
-
Transient Keyword In Java - W3schools.blog
-
Transient Keyword In Java - Tutorialspoint
-
Transient Keyword In Java: What Is It & How It Works? | UpGrad Blog
-
[Java] Transient 키워드 의미 - 데이터 엔지니어링