What is the difference between transient variable,volatile variable

Showing Answers 1 - 22 of 22 Answers

puvan

  • Oct 19th, 2005
 

Transient variable can't be serialize.A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory.

  Was this answer useful?  Yes

gundakiran

  • Oct 25th, 2005
 

volatile modifier requests the Java VM to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, AND one or more threads might change that variable's value, AND ALL of the threads do not use synchronization (methods or blocks) to read and/or write the value, then that member variable must be declared volatile to ensure all threads see the changed value.when you don't want to serialize a member variable then you define that variable as transient.

B.Ravindar Reddy

  • Mar 5th, 2006
 

Transient modifier applies to variables only and it is not stored as part of its objects persistent state.

Volatile modifier applies to variables only and tells the compiles that the variable modified by volatile can be changed unexpected by other parts of the programe.

bishnu

  • Oct 9th, 2006
 

Dear sir/madam

i am a biginer in java.I dont know what is volatile modifier & transient modifier can u plz respond me.

                            Bishnu

  Was this answer useful?  Yes

Sudhir Kumar

  • Sep 27th, 2011
 

volatile modifier requests the Java VM to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, AND one or more threads might change that variable's value, AND ALL of the threads do not use synchronization (methods or blocks) to read and/or write the value, then that member variable must be declared volatile to ensure all threads see the changed value.when you don't want to serialize a member variable then you define that variable as transient.

  Was this answer useful?  Yes

abi

  • Oct 1st, 2011
 

Transient variable can't be serialize. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory.

  Was this answer useful?  Yes

rome2all

  • Oct 4th, 2011
 

transient means witch variable you don't want to be serializable put that variable as transient and volatile variable is variable witch is always changing its value,but its depreciated no use of it,

  Was this answer useful?  Yes

pawan

  • Jan 18th, 2012
 


The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized. Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.

  Was this answer useful?  Yes

Sanjeev

  • Jan 25th, 2012
 

If you are working with the multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when its updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesnt know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value.

sampra

  • Mar 6th, 2012
 

transient variable is the variable which cant be serialized..volatile is useful in multi thread env.each thread has ots own copy to update..in fact it wont update the main memory var ..its update the local cache so if ..it update the main memory only after the completing operation so ..new thread will get the updated value

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions