Java AtomicInteger getAndSet( ) method

The getAndSet() method in Java AtomicInteger class sets the new value to the given atomicInteger and returns the old value.

Syntax

Parameters

newValue: It is new value.

Return

It will return the current value of atomicInteger.

Example 1

Test it Now

Output:

the updated number: 20
the old number: -10

Example 2

Test it Now

Output:

the updated number: 20
the old number: 0

Example 3

Test it Now

Output:

the updated number: 200
the old number: 100

Example 4

Test it Now

Output:

the updated number: 2
the old number: 10

Example 5

Test it Now

Output:

the updated number: -3
the old number: 10





Latest Courses