What is the AtomicInteger.intValue method in Java ?

AtomicInteger represents an int value the may be updated atomically (Atomic operation is performing a single unit of work on a resource. During that operation, no other operations are allowed on the same resource until the performing operation is finished).

AtomicInteger is used in multi-threading where more than one threads try to access the critical section. AtomicInteger has its own intrinsic locks. The AtomicInteger is present in the java.util.concurrent.atomic package.

The intValue method of the AtomicInteger will return the AtomicInteger value as an integer value.

Syntax

public int intValue()

This method doesn’t take any argument.

This method returns the AtomicInteger object’s numeric value as an int value.

Code

The below code demonstrates how to use the intValue method:

import java.util.concurrent.atomic.AtomicInteger;
class IntValue{
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);
int intVal = atomicInteger.intValue();
System.out.println("\natomicInteger.intValue() : " + intVal);
}
}

Explanation

In line number 1: We have imported the AtomicInteger class.

In line number 4: Created a new object for the AtomicInteger class with the name atomicInteger and with the value 10.

In line number 9: Used the intValue method to get the value of the atomicInteger object as an integer value.

Free Resources