Java AtomicInteger getAndIncrement() method

The getAndIncrement() method of Java AtomicInteger class gets the value first and increment it by one.

Syntax

Return

This method returns the previous value.

Example 1

Test it Now

Output:

the updated number: 1
the previous number: 0

Example 2

Test it Now

Output:

the updated number: 11
the previous number: 10

Example 3

Test it Now

Output:

the updated number: 2
the previous number: 1

Example 4

Test it Now

Output:

the updated number: 0
the previous number: -1

Example 5

Test it Now

Output:

the updated number: -3
the previous number: -4





Latest Courses