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 AtomicInteger class.
  • Line 4: We create a new object for the AtomicInteger class, with the name atomicInteger and the value 10.
  • Line 5: We use the get method of the atomicInteger, store it to the integer variable val, and then print the value.

Free Resources