Wednesday, March 22, 2017

Difference between transient and volatile keyword in Java

Surprisingly "Difference between transient and volatile keyword in Java" has asked many times on various java interviews. volatile and transient are two completely different keywords from different areas of Java programming language. the transient keyword is used during serialization of Java object while volatile is related to the visibility of variables modified by multiple threads during concurrent programming. The only similarity between volatile and transient is that they are less used or uncommon keywords and not as popular as public, static, or final.

Anyway, its good to know what transient keyword do in Java or how to use volatile keyword in Java. In this article, we will couple of points between volatile and transient which can be used to answer this interview question.

Difference between transient and volatile keyword in JavaThis article is in continuation of earlier interview questions on serialization like the difference between Serializable and Externalizable and  Top 10 Java serialization interview questions. If you haven’t read them already you may find them useful and interesting.

And, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at the Java Multithreading, Concurrency, and Performance Optimization course by Michael Pogrebinsy on Udemy. It's an advanced course to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance



Difference between transient and volatile keyword in Java



Difference between volatile and transient keyword in Java

1) transient keyword is used along with instance variables to exclude them from the serialization process. if a field is transient its value will not be persisted. see my post on what is a transient keyword in java for more details. 

On the other hand, the volatile keyword can also be used in variables to indicate compiler and JVM that always read its value from the main memory and follow happens-before relationship on the visibility of volatile variables among multiple threads. see my post on how and when to use volatile keyword in Java for more details.



2) transient keyword can not be used along with static keyword but volatile can be used along with static.

3) transient variables are initialized with default value during de-serialization and there assignment or restoration of value has to be handled by application code.



That’s all on the difference between transient and volatile keyword in java. As I said this interview question doesn’t really test you and just try to find whether you are familiar with those less known keywords in java or not. Let us know if you come across any other difference between volatile and transient keyword in java.

Further Learning
Multithreading and Parallel Computing in Java
Java Concurrency in Practice - The Book
Applying Concurrency and Multi-threading to Common Java Patterns
Java Concurrency in Practice Course by Heinz Kabutz

Other discussions on Java interview questions you may like

9 comments :

suraj said...

transient keyword CAN be used along with static keyword...

Javin @ JDBC drivers in Java said...

@Suraj, syntactically yes transient can be used with static but there is no point of doing that as transient keyword is used to prevent any field from serialization and static variables are not serialized anyway. By the way thanks for bringing this point.

Unknown said...

my query is where JRE maintain .class file for annotation

Anonymous said...

@Javin : even though it doesn't make any sense but writing that transient can't be used with static itself is wrong.

javin paul said...

@Anonymous, if you are thinking from compiler's point of view then yes, you can pair static and transient together, but why would you do that? transient is used to prevent a field from being serialized and static fields are not serialized anyway.

Anonymous said...

@Javin Wouldn't it make more sense to say that using the transient keyword with the static keyword is redundant, instead of not possible?

Anonymous said...

@Javin the author needs to modify that statement, i thought it would throw compilation error, but it works normally .

Anonymous said...

if i dont want to use transcient keyword ,then any other option not to make variable serialised.

javin paul said...

@Anonymous, you can make them static if your logic allows that.

Post a Comment