- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between transient and volatile in Java?
transient: An instance variable is marked transient to indicate the JVM to skip the particular variable when serializing the object containing it. This modifier is included in the statement that creates the variable, preceding the class or data type of the variable.
Example
public transient int limit = 55; // will not persist public int b; // will persist
volatile: The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.
Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.
Example
public class MyRunnable implements Runnable { private volatile boolean active; public void run() { active = true; while (active) { } } public void stop() { active = false; } }
- Related Articles
- Difference between volatile and transient in java
- What is the difference between Volatile and Non-Volatile substances?
- Difference between Volatile Memory and Non-Volatile Memory
- What does the modifier transient in Java do?
- What is the difference between Java and Core Java?
- What is the difference between Java and Java EE
- What is the difference between /* */ and /** */ comments in Java?
- What is the difference between >> and >>> operators in Java?
- What is the difference between Java and JavaScript?
- What are Transient variables in Java? Explain.
- What does the modifier volatile in Java do?
- What is the difference between compositions and aggregations in Java?
- What is the difference between abstraction and encapsulation in Java?
- What is the difference between Serialization and Deserialization in Java?
- What is the difference between System.out.println() and System.out.print() in Java?

Advertisements