AtomicIntegerArray toString() method in Java with Examples

The built-in Java function java.util.concurrent.atomic.AtomicInteger.toString() yields the string representation of the value that is currently stored in the integer.

An array's current values are represented by a string that is produced by AtomicIntegerArray's toString() function. Since it makes it simple to see the contents of the atomic array, this is helpful for debugging or logging purposes.

Syntax:

Parameters: There are no parameters that the function accepts.

Return value: The current value as represented in strings is returned by the function.

Example 1:

The use of the AtomicInteger class and its toString method is demonstrated in the Java code given. The initial value of an AtomicInteger called value is 1. The AtomicInteger's current value is displayed on the terminal. The AtomicInteger is then given the toString method call to convert its value to a string, which is subsequently saved in the variable str. Lastly, the console receives a print of the AtomicInteger's string representation. The usage of AtomicInteger to securely store an integer value in a concurrent context is demonstrated in this example.

Implementation:

FileName: ToStringIntegerExample1.java

Output:

 
The Previous value is : 1
The String value is: 1   

Example 2:

The Java code that has been given shows how to change an AtomicInteger into a String. 2000000000 is the initial value of an AtomicInteger object. System.out.println implicitly calls the toString function of value to print the current value of the variable. Value is then explicitly converted to a string by the code, which subsequently puts it in the variable str. Ultimately, it outputs the AtomicInteger's string representation. This shows how to utilize AtomicInteger in output statements by converting it to a string.

Implementation:

FileName: ToStringIntegerExample2.java

Output:

 
The Previous value is : 2000000000
The String value is: 2000000000