AtomicInteger getAndAdd() method in Java with examples Last Updated : 29 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The java.util.concurrent.atomic.AtomicInteger.getAndAdd() is an inbuilt method in java that adds the given value to the current value and returns the value before updation which is of data-type int. Syntax: public final int getAndAdd(int val) Parameters: The function accepts a single mandatory parameter val which specifies the value to be added to the current value. Return value: The function returns the value before addition is performed to the previous value. Program below demonstrates the function: Program 1: Java // Java program that demonstrates // the getAndAdd() function import java.util.concurrent.atomic.AtomicInteger; public class GFG { public static void main(String args[]) { // Initially value as 0 AtomicInteger val = new AtomicInteger(0); // Adds 7 and gets the previous value int res = val.getAndAdd(7); // Prints the updated value System.out.println("Previous value: " + res); System.out.println("Current value: " + val); } } Output: Previous value: 0 Current value: 7 Program 2: Java // Java program that demonstrates // the getAndAdd() function import java.util.concurrent.atomic.AtomicInteger; public class GFG { public static void main(String args[]) { // Initially value as 18 AtomicInteger val = new AtomicInteger(18); // Prints the updated value System.out.println("Previous value: " + val); // Adds 8 and gets the previous value int res = val.getAndAdd(8); // Prints the updated value System.out.println("Previous value: " + res); System.out.println("Current value: " + val); } } Output: Previous value: 18 Previous value: 18 Current value: 26 Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html#getAndAdd-int- Comment More info G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicInteger Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like