• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transient and volatile variables ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I've basic knowledge of java and preparing for SCJP, I'm referring the book by kathy sierra.

I've question regarding transient and volatile variables in java. Please let me know if my understanding is correct:

Transient variables wont be stored/persisted in database, like other instance variables do. Is this correct?

Is 'transient' used only during serialization or in any other scenario ?

I dont understand the concept of 'volatile' variables. Please help with my queries or direct me if these things are discussed in other threads..

Thanks
-Uday
 
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Uday!

Transient variables are indeed not considered when an object is being serialized. It is usually used for variables that cache the result of a method that may be called often. For example, if you have an expensive hashCode() method, you may calculate the hash once and store it in a variable and return that value every time the hashCode() method is called after that. When the object is being serialized, the process is free to discard the value, because it can be easily be recomputed again.

Volatile variables tell the JVM that threads are not allowed to store values assigned to them into cache, but have to write it to memory immediately. It ensures that different threads of execution see the same value when they query the variable. It's a rarely used keyword, since it is only useful in very specific conditions.
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the transient variable:

If a class has a transient variable and you are using multi threading ,then each thread must reconcile its copy of a transient variable from main memory.
If any thread using the transient variable modifies that variable then the thread must have to write it to memory immediately, and the other threads must reconcile their copy
of the variable from main memory before using it.

Thanks..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's volatile, not transient.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaa thanks..

while posting reply ,may be Im thinking something else!!!

Thanks again
 
uday mane
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information Stephan and Vijay

I have found one more view to understand volatile :

volatile is usually used in threads. In threading there are two types of memory. Main memory and threads’ local memory.
Threads’ local memory is like a screen shot of main memory. So screenshot is not necessarily the current state of the main memory but the state when screenshot is taken.
volatile tells the thread not use screenshot of variable but use main memory instead which is always updated.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic