AtomicInteger intValue() method in Java with examples
The intValue()
method in Java is a part of the AtomicInteger
class. It returns the current value of the AtomicInteger
as an int
.
Syntax:
public int intValue()
Example 1:
AtomicInteger atomicInt = new AtomicInteger(10);
int value = atomicInt.intValue();
System.out.println("Value of atomicInt: " + value);
Output:
Value of atomicInt: 10
Example 2:
AtomicInteger atomicInt = new AtomicInteger();
atomicInt.set(20);
int value = atomicInt.intValue();
System.out.println("Value of atomicInt: " + value);
Output:
Value of atomicInt: 20
Example 3:
AtomicInteger atomicInt = new AtomicInteger(5);
int value1 = atomicInt.intValue();
atomicInt.addAndGet(10);
int value2 = atomicInt.intValue();
System.out.println("Value1 of atomicInt: " + value1);
System.out.println("Value2 of atomicInt: " + value2);
Output:
Value1 of atomicInt: 5
Value2 of atomicInt: 15
In the above example, we first get the value of atomicInt
using intValue()
method and store it in value1
. Then we add 10 to atomicInt
using addAndGet()
method and get the new value of atomicInt
using intValue()
method and store it in value2
.