This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Java in General and the fly likes variables difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "variables difference" Watch "variables difference" New topic

variables difference

amit bhadre
Ranch Hand

Joined: Jan 23, 2006
Posts: 124
What is the difference b/w transient and volatile variable?

Thanks advance who ever answers this question.

amit bhadre
Henry Wong

Joined: Sep 28, 2004
Posts: 19531

A transient variable is an instance variable that will not be processed during serialization of the object that it is part of. This means that when the object is deserialized, the variable will have a default value, instead of the original value.

A volatile variable is a variable that will be accessed by multiple threads at the same time. The JVM will not make cache copies of the variable. Furthermore, the JVM will also make sure that reads and writes to the volatile variable is atomic -- which for longs and doubles is not normally the case.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Consider Paul's rocket mass heater.
subject: variables difference