Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transient & Volatile

 
Tom Keith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can somebody tell me what is the purpose of using
Transient & Volatile keywords? and also how and where can it be used in a java program? Does it have any significance?
Thanks.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,
Transient comes into play regarding serialization. A transient variable will not be serialized. This is important, because some class state variables don't need to be persistent. For example, suppose you had the following class:

The currentTemperature will probably be different when the class is restored. Therefore, we don't want to waste file space saving a variable that will change anyway.
Volatile relates to threading and optimization. When you declare a variable to be volatile, you are telling the compiler not to try and optimize (sharing) it regarding threading. I don't really have an example for this because I have not used it.
Regards,
Manfred.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is one of the uses of volatile - to solve the memory issues of double-checked locking. I could be wrong though, so please don't trust me.

[ April 01, 2002: Message edited by: David Weitzman ]
 
Mike Birken
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone care to comment on strictfp?
- Mike
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictfp is just like volatile -- you'll probably never actually need to use it. Actually you're more likely to use volatile. It just means that arithmatic will be done exactly according to some standard or other (a standard that has also been implemented in other languages like C++). With strictfp, the answer to 345/235.63 will always be exactly the same no matter what vitual machine and operating system you calculate it on. Without strictfp, there's an allowed margin of error. It's sort of the counterpart to the java.lang.StrictMath. Just another feature that will never be used.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic