Java - Interview Questions and Answers on Volatile keyword

Q1.  What is Volatile keyword used for ?

Ans. Volatile is a declaration that a variable can be accessed by multiple threads and hence shouldn't be cached.

Q2.  How does volatile affect code optimization by compiler?

Ans. Volatile is an instruction that the variables can be accessed by multiple threads and hence shouldn't be cached. As volatile variables are never cached and hence their retrieval cannot be optimized.