What is the use of transient and voatile keyword in java ? In which situation we used transient and volatile keywords ?
I check it google search but i am not clear about their definitions..
---------------------
suresh
What is the use of transient and voatile keyword in java ? In which situation we used transient and volatile keywords ?
I check it google search but i am not clear about their definitions..
---------------------
suresh
volatile keyword is used for variables that are changed by multiple threads. it instructs the processor to fetch new copy every time. not the catched value.
volatile keyword tell the the compiler that the variable modified by "volatile" can be cahnged unexpectedly by other parts of ur program
Transient:when a variable is declared as transient ,thenits value need not persist when the object is stored.
bye
:)
NEVER SAY DIE.
Thanks barbie and shenoy.....can you tell me where it is( or which type of projects) mainly used ?
----------------
suresh
Variables may be marked transient to indicate that they are not part of the persistent state of an object.
For eg
If an instance of the class abc:
class abc {
int x, y;
transient l,m;
}
were saved to persistent storage by a system service,then only fields x and y will be saved.The variables l,and m are not serialized.
Volatile:Volatile is generally used for handling THREADS effectively,
For example Specifying a variable as volatile tells
the JVM that any threads using that variable are not allowed to cache(hide) that
value at all. Making the Variable "Volatile " will ensure that the compiler
will get the content of the variable every time it is used and not cache its
content.
bye
:)
NEVER SAY DIE.
hi suresh
u wanna want to know about transient
when we serialize any object its all data members are serialized except the data members declared as transient.... fine.
now about volatile..
volatile is a keyword that must be applied when declaring any variable that will reference a device register. Without the use of volatile, the compile-time optimizer can inadvertently delete important accesses. Neglecting to use volatile might result in bugs that are difficult to track down.
Thanks for your valuable reply.....
Nice to hear you....
---------------
suresh