What is the AtomicInteger.longValue method in Java ?

AtomicInteger represents an int value the may be updated atomically

Atomic operation is performing a single unit of work on a resource. During that operation, no other operations are allowed on the same resource until the performing operation is finished.

The AtomicInteger is present in the java.util.concurrent.atomic package.

The longValue() method of the AtomicInteger will return the AtomicInteger value as a long value.

Syntax

public long longValue()

This method doesn’t take any argument.

This method returns the AtomicInteger object’s numeric value as a long value.

Code

The code below demonstrates how to use the longValue() method:

import java.util.concurrent.atomic.AtomicInteger;
class LongValue{
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger(10);
int val = atomicInteger.get();
System.out.println("The value in the atomicInteger object is " + val);
long longVal = atomicInteger.longValue();
System.out.println("\natomicInteger.longValue() : " + longVal);
}
}

#3

Explanation

In line 1: We import the AtomicInteger class.

In line 4: We create a new object for the AtomicInteger class with the name atomicInteger and with the value 10.

In line 9: We use the longValue() method to get the value of the atomicInteger object as a long value.

Free Resources