Difference between transient and volatile keyword in Java

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.


Difference between transient and volatile keyword in JavaThis 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.

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 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.


Read more: http://javarevisited.blogspot.com/2012/03/difference-between-transient-and.html#ixzz2ki7kdGRo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值