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 the AtomicLong class.

  • In line 4: We create a new object for the AtomicLong class with the name atomicLong and value 10.

  • In line 5: We use the toString() method to get the value of the AtomicLong object as a string.

Free Resources