DoubleAdder intValue() method in Java with Examples

The Java has a built-in function called DoubleAdder.intValue() that, adhering to a narrowing primitive conversion, returns the sum() as an int. The class object's initial value is zero when it is formed.

Syntax:

Parameters: There are no parameters that may be given with this method.

Return Value: If this object is converted to an int data type, the method returns the numeric value that it represents.

Example 1:

The DoubleAdder class from the java.util.concurrent.atomic library is used in the provided Java application as an example. The add method is used in the main method to add the numbers 10 and 50 to an instance of DoubleAdder called n. When the intValue() method is invoked on n, the current sum is converted to an int; however, neither the result nor its use is kept.

Implementation:

FileName: SumOfNumbers.java

Output:

The value after performing the intValue() is: 60.0

Example 2:

The Java program that is given shows how to create a multithreaded application that uses the DoubleAdder class to execute additions simultaneously. It generates two threads and a DoubleAdder object called add_operation. Every thread executes a ten-time loop, wherein thread 1 adds 10.0 and thread 2 adds 20.0 to the DoubleAdder.

Implementation:

FileName: MultithreadUsingDoubleAdder.java

Output:

The total Sum as double: 300.0
The total Sum as int: 300