What is the AtomicLong.toString method in Java?
Overview
AtomicLong represents a long value the may be updated atomically.
An atomic operation performs 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 complete.
AtomicLong is present in the java.util.concurrent.atomic package.
The toString method of = AtomicLong returns the AtomicLong value as a String value.
Syntax
public String toString()
Parameters
This method doesn’t take any arguments.
Return value
The toString() method returns the String representation of the AtomicLong object’s value.
Code
The code below demonstrates how to use the toString() method:
import java.util.concurrent.atomic.AtomicLong;class StringValue{public static void main(String[] args) {AtomicLong atomicLong = new AtomicLong(10);System.out.println("The String value of atomicLong is - " + atomicLong.toString());}}
Explanation
-
In line
1: We import theAtomicLongclass. -
In line
4: We create a new object for theAtomicLongclass with the nameatomicLongand value10. -
In line
5: We use thetoString()method to get the value of theAtomicLongobject as a string.