GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 458 of 502    Print  
Transient and Volatile.
What is the difference between Transient and Volatile. Does volatile variable is serialized and synchronised.


  
Total Answers and Comments: 7 Last Update: August 14, 2009     Asked by: sha_kr2001 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sampra
 
transient var cant serilizabe
volatile varibale can be changed




Above answer was rated as good by the following members:
interviewprep9, phdsong, Djava, bino75
January 02, 2008 09:33:20   #1  
rahultiwari8aug Member Since: January 2008   Contribution: 3    

RE: Transient and Volatile.
transient and volatile both are used to define non -serializable members of any class.means if we want to convert an object into byte stream and write into file we serialize the class but if any variable or method which we dont want to serialize we can specify them by above.note---VOLATILE is used with variable andTRANSIENT is used with methods.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
January 03, 2008 06:41:29   #2  
sha_kr2001 Member Since: December 2007   Contribution: 7    

RE: Transient and Volatile.
Thanks for your answer.But according to u if Volatile is not serialized then how it can be used for Shared variable.Thant means how the values of volatile var can be modified....
 
Is this answer useful? Yes | No
January 06, 2008 12:34:43   #3  
rahultiwari8aug Member Since: January 2008   Contribution: 3    

RE: Transient and Volatile.
Volatile is assigned with variable when we synchronze that variable in multi threading environment.It also indicate to have a master copy of it that is keep an updated value of this variable.
Transient we use when we want to serialize and object and that object keeps any thread or socket or any other type of member present in that object then we have to specify them transient so that JVM does not try to serialize them otherwise error will be thrown.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
February 11, 2008 06:25:14   #4  
sampra Member Since: February 2008   Contribution: 278    

RE: Transient and Volatile.
transient var cant serilizabe
volatile varibale can be changed



 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 16, 2008 15:55:14   #5  
interviewprep9 Member Since: June 2008   Contribution: 10    

RE: Transient and Volatile.
Both are used for only intsnace variables.
 
Is this answer useful? Yes | No
June 17, 2008 01:30:24   #6  
atmarammuduli Member Since: December 2007   Contribution: 8    

RE: Transient and Volatile.
Volatile:
Threads dont cache method copy of Volatile instance variables. Each time when the var is used the thread gets latest copy.

Transient:
instance var not serialized ever. After deserialization its value is null(object) or zero(primitive type).


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
August 14, 2009 06:11:56   #7  
Sarje Member Since: August 2009   Contribution: 62    

RE: Transient and Volatile.

Transient State of an object can be saved (write to any file) if the class
implements java.io.Serializable interface. In this case all of the field's value
will be saved. If we want any of the field's state must not be saved then we
have to modify that field by transient modifier. For example:


public class SerialDemo implements
java.io.Serializable

{

String name;

double salary;

transient int dob;

}


When we serialize the object of this class then name and salary field's value
will be saved but the value of dob will not be saved.


Volatile: In a multithreaded program sometimes two or more threads share
same variable. For efficiency each thread keeps its own private copy of the
shared variable and work on it. If you want to restrict the thread from
using own private copy of the variable and let them use master copy of the
shared variable then modify variable using volatile modifier. In
short we can say that volatile modifier can not be cached by the threads.


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape