What is Transient and volatile

Answers were Sorted based on User's Feedback



What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

What is Transient and volatile..

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

Post New Answer

More Core Java Interview Questions

How to deprecate a method? Show it with proper example. Plz give the answer of this.Thanx in advance. mail me: tanzeem.akhtar@gmail.com

2 Answers  


The following program reads data (details of students) from a file named students.txt and converts it into e-mail addresses. The results are written to a file named studentemail.txt. students.txt consists of a number of lines, each containing the data of a student in colon delimited format: Last Name:First Name:Student Number Each input record is converted to an e-mail address and written to studentemail.txt in the following format: the first character of the last name + the first character of the first name + the last four digits of the student number + “@myunisa.ac.za” import java.io.*; public class EmailConverter { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader ("students.txt")); PrintWriter output = new PrintWriter(new FileWriter ("studentemail.txt")); String line = input.readLine(); while (line != null) { // Extract the information for each student String[] items = line.split(":"); // Generate the email address String email = "" + items[0].charAt(0) + items[1].charAt(0) + items[2].substring(4,8) + "@myunisa.ac.za"; email = email.toLowerCase(); // Output output.println(email); line = input.readLine(); } input.close(); output.close(); } } Rewrite the class so that it handles possible errors that may occur. In particular, it should do the following: • It should catch at least three appropriate exceptions that might occur, and display suitable messages. • At this stage, the program will not run correctly if there is an empty line in the input file. Change the program so that if an empty line is encountered, an exception is thrown and the empty line is ignored. This exception should be handled with the display of a suitable error message. • Before the e-mail address is added to the output file, check if the student number has 8 digits. If not, throw an InvalidFormatException (which the program should not handle itself)

0 Answers  


What is the order of arraylist in java?

0 Answers  


What are mutable classes?

0 Answers  


suppose string s1="rajnish"; string s2="bhaskar"; then what will be happend ?

9 Answers   Fidelity,






What modifiers may be used with a top-level class?

0 Answers  


How do you define a singleton class?

0 Answers  


How does Vector implement synchronization?

4 Answers   Ness Technologies,


Is there is any difference between a scrollbar and a scrollpane?

0 Answers  


What is the nested interface?

0 Answers  


23. Storage space in java is of the form Stack Queue Heap List 24. What is java code embedded in a web page known as Applets Servlets scriptlets snippets 25. Which of the following attributes are compulsory with an <applet> tag?. code,height & width. 26. What does 'CODEBASE' in an applet tag specify?. Files absolute path.

0 Answers   TCS,


Explain the importance of thread scheduler in java?

0 Answers  


Categories