Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

decrementandget
atomicinteger
java
communitycreator

What is the AtomicInteger.decrementAndGet method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

AtomicInteger represents an int value that may be updated atomicallyAtomic 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.. The 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.

Syntax

public final int decrementAndGet()

Parameters

This method doesn’t take any argument.

Return value

This method returns the decremented value as an integer.

Code

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);
    
  }
}
decrementAndGet() method.

Explanation

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).

RELATED TAGS

decrementandget
atomicinteger
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring