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 infoAdvertise with us Next Article AtomicInteger getAndAdd() method in Java with examples gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicInteger Practice Tags : Java Similar Reads AtomicIntegerArray getAndAdd() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.getAndAdd() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicIntegerArray. This method takes the index value of the AtomicIntegerArray and the value to be added as the parameters and returns t 3 min read AtomicInteger getAndSet() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.getAndSet() is an inbuilt method in java that sets the given value to the value passed in the parameter and returns the value before updation which is of data-type int. Syntax: public final int getAndSet(int val) Parameters: The function accepts a single 2 min read AtomicInteger get() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.get() is an inbuilt method in java which returns the current value which is of date-type int. Syntax: public final int get() Parameters: The function does not accepts any parameter. Return value: The function returns the current value Program below demon 1 min read AtomicLong getAndAdd() method in Java with examples The Java.util.concurrent.atomic.AtomicLong.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 long. Syntax: public final long getAndAdd(long val) Parameters: The function accepts a single mandatory param 2 min read AtomicLongArray getAndAdd() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.getAndAdd() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicLongArray. This method takes the index value of the AtomicLongArray and the value to be added as the parameters and returns the value 3 min read Like