What is the AtomicBoolean.toString() method in Java?

AtomicBoolean represents a boolean value that can be updated atomicallyAn atomic operation performs a single unit of work on a resource. No other operations are allowed on the same resource until the performing operation is finished..

AtomicBoolean is present in the java.util.concurrent.atomic package.

The toString() method of AtomicBoolean returns the AtomicBoolean value as a String value.

Syntax

public String toString()

Parameters

This method doesn’t take any parameters.

Return value

The toString() method returns the String representation of the AtomicBoolean object’s value.

Code

The code below demonstrates how to use the toString() method.

import java.util.concurrent.atomic.AtomicBoolean;
class StringValue{
public static void main(String[] args) {
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
System.out.println("The String value of atomicBoolean is - " + atomicBoolean.toString());
}
}

Explanation

  • Line 1: We import the AtomicBoolean class.

  • Line 4: We create a new object for the AtomicBoolean class with the name atomicBoolean and the value 10.

  • Line 5: We use the toString() method to get the value of the atomicBoolean object as a string.

Free Resources