Thursday, April 13, 2023

Difference between transient vs volatile variable or modifier in Java

transient vs volatile modifier in Java
What is the difference between transient and volatile variables or modifiers in Java is one of the most common Serialization Interview Questions in Java. Though volatile variables are not related to Serialization at all, this question is mostly asked in conjunction with other Serialization questions. Both transient and volatile modifiers are completely different from each other. In fact, this question is as popular as Serializable vs Externalizable in Java. The main difference between transient vs volatile variables is that transient variables are not serialized during the Serialization process in Java while volatile variables are used to provide alternative synchronization in Java. 

By using volatile keyword or modifier on fields, signals compiler that this variable is accessed by multiple threads and any reordering or optimization by compiler should be avoided. In this Java article, we will see some differences between transient and volatile variables by learning each of them and When to use a transient and volatile variable in Java.


Difference between transient vs volatile variables Java

transient vs volatile modifier and variable in javaLet's see the difference between transient and volatile variables in point format for easy understanding and revision :



1) By making a variable or field transient in a Class prevents it from being Serialized in Java. Along with static variables, transient variables are not serialized during Serialization and they are initialized with
there default value during the deserialization process e.g. an int transient variable is initialized with zero during deserialization in Java. to learn more about transient variables, See What is transient variable in Java.

2) On the other hand volatile variables are used in Concurrent programming in Java. When we declare a variable volatile, every thread reads its value from the main memory and doesn't use the cached value available in every thread stack. volatile variable also prevents the compiler from doing reordering which can compromise synchronization. to learn more about volatile variables, read What is a volatile variable in Java

Many Java programmers though know about volatile variables they are not sure where to use volatile modifiers in Java. One of the popular examples of using volatile variables is implementing double-checked locking in Singleton, where the Singleton instance of the class is declared volatile in Java.

That's all about the difference between transient and volatile variables in Java. transient variables are used to prevent serialization or a field while volatile variables are used to prevent reordering and avoid the reading cached value of a field in a multithreaded Java program. 

The only similarity between transient and volatile keywords is that they are only applicable to fields or properties of the class. You can not use transient or volatile keywords during class or method declaration in Java.

Other Java Interview questions from java67
Difference between Serializable and Externalizable in Java
Difference between Abstraction and Encapsulation in Java
Difference between Abstract class and Interface in Java
Difference on Hashtable vs HashMap in Java
Difference on ArrayList vs Vector in Java

1 comment:

Feel free to comment, ask questions if you have any doubt.