---
title: "What is the use of voltile variable in java"  
description: "What is the use of voltile variable in java"  
author: "Anonymous User"  
published: 2015-09-04  
updated: 2020-09-22  
canonical: https://www.mindstick.com/interview/22803/what-is-the-use-of-voltile-variable-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# What is the use of voltile variable in java

The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.\
The Java volatile keyword guarantees visibility of changes to variables across threads. In a multithreaded application where the threads operate on non-volatile variables, each thread may copy variables from main memory into a CPU cache while working on them, for performance reasons. If your computer contains more than one CPU, each thread may run on a different CPU. That means, that each thread may copy the variables into the CPU cache of different CPUs.

## Answers

### Answer by Anonymous User

The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.\
The Java volatile keyword guarantees visibility of changes to variables across threads. In a multithreaded application where the threads operate on non-volatile variables, each thread may copy variables from main memory into a CPU cache while working on them, for performance reasons. If your computer contains more than one CPU, each thread may run on a different CPU. That means, that each thread may copy the variables into the CPU cache of different CPUs.


---

Original Source: https://www.mindstick.com/interview/22803/what-is-the-use-of-voltile-variable-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
