Surprisingly "Difference between transient and volatile keyword in Java" has asked many times on various java interview. volatile and transient are two completely different keywords from different areas of Java programming language. transient keyword is used during serialization of Java object while volatile is related to visibility of variables modified by multiple thread during concurrent programming. 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.
This article is in continuation of earlier interview question on serialization like difference between Serializable and Externaliable and Top 10 Java serialization interview question. If you haven’t read them already you may find them useful and interesting.
Difference between volatile and transient keyword in Java
1) transient keyword is used along with instance variables to exclude them from serialization process. if a field is transient its value will not be persisted. see my post what is transient keyword in java for more details. On the other hand volatile keyword can also be used in variables to indicate compiler and JVM that always read its value from main memory and follow happens-before relationship on visibility of volatile variable among multiple thread. see my post how and when to use volatile keyword in Java for more details.
Why Java doesn’t support multiple inheritance in Java
Why wait and notify are defined in Object class
Why character array is better than String for storing password in Java
Why implementing Runnable better than extending Thread class
Why main method is public static in Java
What is Marker interface and why do you used in Java
No comments:
Post a Comment