AtomicInteger getAndSet() method in Java with examples Last Updated : 29 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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 mandatory parameter val which specifies the value to be updated. Return value: The function returns the value before update operation is performed to the previous value. Program below demonstrates the function: Program 1: Java // Java program that demonstrates // the getAndSet() 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); // Updates and sets int res = val.getAndSet(10); System.out.println("Previous value: " + res); // Prints the updated value System.out.println("Current value: " + val); } } Output: Previous value: 0 Current value: 10 Program 2: Java // Java program that demonstrates // the getAndSet() 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); // Gets and updates int res = val.getAndSet(12); System.out.println("Previous value: " + res); // Prints the updated value System.out.println("Current value: " + val); } } Output: Previous value: 18 Current value: 12 Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html#getAndSet-- Comment More infoAdvertise with us Next Article AtomicInteger getAndSet() method in Java with examples G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicInteger Practice Tags : Java Similar Reads AtomicIntegerArray getAndSet() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.getAndSet() is an inbuilt method in Java that atomically sets a given value at any given position of the AtomicIntegerArray. The method takes the index value of the AtomicIntegerArray and the value to set as the parameters. It returns the value at t 3 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 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 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 getAndDecrement() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.getAndDecrement() is an inbuilt method in java that decreases the given value by one and returns the value before updation which is of data-type int. Syntax: public final int getAndDecrement() Parameters: The function does not accepts a single parameter. 2 min read Like