
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between volatile and transient in java
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.
On the other hand, 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.
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 |
Example of Transient
// A sample class that uses transient keyword to // skip their serialization. class TransientExample implements Serializable { transient int age; // serialize other fields private String name; private String address; // other code }
Example of Volatile
class VolatileExmaple extends Thread{ boolean volatile 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); } }
- Related Articles
- What is the difference between transient and volatile in Java?
- Difference between Volatile Memory and Non-Volatile Memory
- What is the difference between Volatile and Non-Volatile substances?
- transient Keyword in Java
- volatile Keyword in Java
- Can we make Array volatile using volatile keyword in Java?
- What are Transient variables in Java? Explain.
- How transient works with final in Java serialization?
- What does the modifier transient in Java do?
- Why the transient variable is not serialized in Java?
- What does the modifier volatile in Java do?
- Difference between Java and JavaScript.
- Difference between Go and Java.
- Difference Between C++ and Java
- Difference between Groovy and Java

Advertisements