What is the difference between transatile,volatile modifier?

Showing Answers 1 - 1 of 1 Answers

Transient Field

Objects can be stored to persist using serialization. Serialization converts object into an output format that can be used for storing the object. When de-serialized object can be retrieved in the same state as it was when serialized.

When a field is specified as transient, it indicates its value should not be saved when object of the class are written to persist storage.

Example

class TransientTest{

transient int temp = 35;

double volume = 200;

}

when the object of this class is serialized the value of temp is not saved, but value of volume will be saved.

Volatile Fields

During execution, the compile code might be cached for the sake of efficiency reasons. There can be multiple threads accessing the same field. Thus there can be inconsistency between the master copy and the cached version.

Declearation of a field as volatile ensures that the write operation will always be performed on the master copy and read operation will always return the letest value of the field

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions