Tuesday, September 6, 2011

volatile, transient and instanceof in Java...

volatile: a volatile keyword tells the compiler that the variable value can change unexpectedly by other parts of your program. In java sometimes if threads share some variable, for efficiency they all keep their private copy of the shared variable. at certain positions specially in synchronized constructs most updated copy must be referred, so volatile tells the compiler that always use the master copy for volatile variable or keep it as updated as possible.

transient:
a transient variable has two objectives in declaration :
1. it must not be serialized
2. it must not persist on storing it in permanent storage area.
i.e. when you store object in hard disk, it would be composed of combination of values of all its data members except transient variable.

instanceof:
objectRef instsnceof ClassName return boolean. It is an operator which is used to determine the object with the class name. Its not only match it with its ClassName but even with its parent. However it returns false on null.
E.g. class A{}
class B extends A{}

B objB=new B();
A objA=new A();
B objB1=null;
objB instanceof B//returns true
objB instanceof A//returns true
objA instanceof A//returns true
objA instanceof B//returns false
objB1 instanceof B//returns false
objB1 instanceof A//returns false

No comments:

Post a Comment