AtomicInteger
represents an int value that may be updated AtomicInteger
is present in the java.util.concurrent.atomic
package.
The decrementAndGet
method of the AtomicInteger
will atomically decrement the current value by 1 and returns the updated value of the AtomicInteger object.
public final int decrementAndGet()
This method doesn’t take any argument.
This method returns the decremented value as an integer.
The code below demonstrates how to use the decrementAndGet
method:
import java.util.concurrent.atomic.AtomicInteger;class DecrementAndGet{public static void main(String[] args) {AtomicInteger atomicInteger = new AtomicInteger(10);System.out.println("The value in the atomicInteger object is " + atomicInteger.get());System.out.println("\nCalling atomicInteger.decrementAndGet()");int val = atomicInteger.decrementAndGet();System.out.println("The new Value is : " + val);}}
Line 1: We have imported the AtomicInteger
class.
Line 4: Created a new object for the AtomicInteger
class with the name atomicInteger
and with the value 10
.
Line 9: Called the decrementAndGet
method. This method will decrement the current value(10
) by 1
and returns the new value(9
).