What is Transient and volatile
Answers were Sorted based on User's Feedback
Answer / subrahmanyam
transient
identifies a variable not to be written out when an
instance is serialized (It can'nt be copied to remove
system)
volatile
indicates that the field is used by synchronized threads
and that the compiler should not attempt to perform
optimizations with it.
When more than one thread share a (volatile) data it is
checked every time. Every thread keeps the latest value of
volatile variable
Is This Answer Correct ? | 30 Yes | 4 No |
Answer / jagan kumar(zylog systems ltd.
Transient
.........
Only variable can be declared as Transient, When
used as a Modifier in a variable Declaration it suggests
that a variable may not be return out when the class is
serialized
Volatile
.........
The keyword volatile can be used to declare
variables. The use of the keyword volatile in a variable
declaration suggests the compiler that multiple threads may
access the variable. Therefore the value of the variable
may change unexpectedly. A Compile time error will occur
declaring a variable both volatile and final.
Example for Volatile:
.....................
volatile int v = 0;
Thread 1:
v++;
Thread 2:
v--;
The questioners usually want the answer "v can only
be 0 after this code is run", because
volatile int v = 0;
Thread 1:
r1 = v;
r2 = r1 + 1;
v = r2;
Thread 2:
r3 = v;
r4 = r3 - 1;
v = r4;
So, if Threads 1 and 2 both read v and see the value
0, then Thread 1 will write 1 to it and Thread 2 will
write -1 to it. You are not guaranteed to see the value 0!
Is This Answer Correct ? | 16 Yes | 4 No |
Answer / sudhadevi
Transient
~~~~~~~~~
identifies a variable not to be written out when
an instance is serialized (It can'nt be copied to remove
system)
Volatile
~~~~~~~~
indicates that the field is used by synchronized
threads and that the compiler should not attempt to perform
optimizations with it.When more than one thread share a
(volatile) data it is checked every time. Every thread
keeps the latest value of volatile variable
Is This Answer Correct ? | 12 Yes | 2 No |
Answer / lakshmidontukurthy
Trying to put a non-serializable variable in a
seriealisible class,we can use transient modifier ,so that
the jvm skips the transient variable ,and make that class
as serializable class
Is This Answer Correct ? | 9 Yes | 5 No |
Answer / manodeep pandey
Java defines two interesting type modifiers: transient and
volatile. These modifiers are used to handle somewhat
specialized situations.
When an instance variable is declared as transient, then
its value need not persist when an object is stored. For
example:
class T {
transient int a; // will not persist
int b; // will persist
}
Here, if an object of type T is written to a persistent
storage area, the contents of a would not be saved, but the
contents of b would. The volatile modifier tells the
compiler that the variable modified by volatile can be
changed unexpectedly by other parts of your program. One of
these situations involves multithreaded programs. In a
multithreaded program, sometimes, two or more threads share
the same instance variable. For efficiency considerations,
each thread can keep its own, private copy of such a shared
variable. The real (or master) copy of the variable is
updated at various times, such as when a synchronized
method is entered. While this approach works fine, it may
be inefficient at times. In some cases, all that really
matters is that the master copy of a variable always
reflects its current state. To ensure this, simply specify
the variable as volatile, which tells the compiler that it
must always use the master copy of a volatile variable (or,
at least, always keep any private copies up to date with
the master copy, and vice versa). Also, accesses to the
master variable must be executed in the precise order in
which they are executed on any private copy.
Is This Answer Correct ? | 5 Yes | 1 No |
Answer / deepak divvela
1.transient variables can not be serialized.
2.volatile variables may change with the affect of other one
Is This Answer Correct ? | 4 Yes | 2 No |
Answer / pasindu
Trsnsient:
If you mark an instance variable as transient you're telling
the JVM to skip this variable when you attempt to serialize
the object containing it.
Volatile:
The volatile modifier tells the JVM that a thread accessing
the variable must always reconcile its own private copy of
the variable with the master copy in memory.
It can be apply only to instance variables.
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / soumay m cyriac
we cant write the value of a transient variable into
datastream.volatile means its just like temporary variable
Is This Answer Correct ? | 15 Yes | 21 No |
Answer / ravikiran
Transient variable is used to make the variable out of
synchronization.
Volatile variable is used when the variable is changing
it's state continuously
Is This Answer Correct ? | 1 Yes | 11 No |
Answer / loganathan s
both are used for declaring a vaiables.we can say it is a
modifiers.
Is This Answer Correct ? | 5 Yes | 21 No |
Why java doesn’t support multiple inheritances?
How do you sort a string in alphabetical order in java?
what difference between throw and throws in exception handling.
What are aggregate functions explain with examples?
How many bytes is a string?
Why is stringbuffer not immutable?
how to use crystal reports in java
Do we have pointers in java?
Difference between abtsract & final
What does three dots mean in java?
What is a constructor, constructor overloading in java?
Which class is the superclass of all classes?