What is the AtomicInteger.get method in Java?
An AtomicInteger represents an integer value that can update atomically.
An atomic operation performs a single unit of work on a resource. Other operations are not allowed on the same resource until the first operation is finished.
The AtomicInteger class is present in the java.util.concurrent.atomic package.
This article may help us better understand the Atomic concept.
We use the get method of the AtomicInteger to get the value present in the AtomicInteger object.
Syntax
public final int get()
Arguments
This method takes no arguments.
Return value
This method returns the value present in the AtomicInteger object.
Working example
The below code demonstrates how to use the get method:
import java.util.concurrent.atomic.AtomicInteger;class Get{public static void main(String[] args) {AtomicInteger atomicInteger = new AtomicInteger(10);int val = atomicInteger.get();System.out.println("The value in the atomicInteger object is " + val);}}
Explanation
- Line 1: We import the
AtomicIntegerclass. - Line 4: We create a new object for the
AtomicIntegerclass, with the nameatomicIntegerand the value10. - Line 5: We use the
getmethod of theatomicInteger, store it to the integer variableval, and then print the value.