AtomicLong getAndAdd() method in Java with examples Last Updated : 29 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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. Below programs illustrate the above method: Program 1: Java // Java program that demonstrates // the getAndAdd() function import java.util.concurrent.atomic.AtomicLong; public class GFG { public static void main(String args[]) { // Initially value as 0 AtomicLong val = new AtomicLong(0); // Adds 7 and gets the previous value long 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.AtomicLong; public class GFG { public static void main(String args[]) { // Initially value as 18 AtomicLong val = new AtomicLong(18); // Prints the updated value System.out.println("Previous value: " + val); // Adds 8 and gets the previous value long 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/AtomicLong.html#getAndAdd-long- Comment More infoAdvertise with us Next Article AtomicLong getAndUpdate() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicLong Practice Tags : Java Similar Reads 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 AtomicLong getAndSet() method in Java with examples The Java.util.concurrent.atomic.AtomicLong.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 long. Syntax: public final long getAndSet(long val) Parameters: The function accepts a single 2 min read AtomicInteger getAndAdd() method in Java with examples 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 param 2 min read AtomicLong getAndUpdate() method in Java with Examples The Java.AtomicLong.getAndUpdate() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value. It takes an object of LongUnaryOperator interface as its parameter and applies the operation specified in the object to the current 1 min read AtomicLong getAndUpdate() method in Java with Examples The Java.AtomicLong.getAndUpdate() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value. It takes an object of LongUnaryOperator interface as its parameter and applies the operation specified in the object to the current 1 min read AtomicLong get() method in Java with examples The Java.util.concurrent.atomic.AtomicLong.get() is an inbuilt method in java which returns the current value which is of date-type long. Syntax: public final long get() Parameters: The function does not accepts any parameter. Return value: The function returns the current value Below programs illus 1 min read Like