Just like any other programming language, Java has a set of keywords which are reserved and have a special meaning. In this article, we will see the difference between the keywords volatile and transient.
Before getting into the differences, let us first understand what each of them actually means.
The Volatile keyword is used to mark the JVM and thread to read its value from primary memory and not utilize cached value present in the thread stack. It is used in concurrent programming in java.
The Transient keyword is used with the instance variable to eliminate it from the serialization process. During serialization, the value of the transient field or variable is not saved.
The following table describes the differences:
|The Transient marked variable prevents it from being serialized.||The Volatile marked variable follows happen-before relationship on visibility in multithreaded java program which reduces the risk of memory consistency errors.|
|It gives control and flexibility over to exclude some object methods from serialization process.||It prevents JVM from doing re-ordering which could compromise synchronization.|
|During deserialization they are initialized with a default value.||They are not initialized with a default value.|
|It cannot be used with the static keyword as static variable do not belong to individual instance. During serialization, only object’s current state is concerned.||It can be used with the static keyword.|
|It cannot be used with the final keyword. Although JVM doesn’t complain about it but during deserialization one will face problem of reinitializing a variable.||It can be used with the final keyword.|
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Difference between Volatile Memory and Non-Volatile Memory
- transient keyword in Java
- volatile keyword in Java
- super and this keywords in Java
- Important Keywords in Java
- List of all Java Keywords
- Difference between Core Java and Advanced Java
- Difference between a Java Application and a Java Applet
- Difference between Java IO and Java NIO
- Difference between Java and Core Java
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Similarities and Difference between Java and C++
- Difference between Scanner and BufferReader Class in Java
- Difference between Abstract Class and Interface in Java
- Difference between == and .equals() method in Java
- Difference between x++ and x=x+1 in Java
- Difference between super() and this() in java
- Difference between Simple and Compound Assignment in Java
- Difference between throw and throws in Java
- Difference between notify() and notifyAll() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.