Open In App

LongAdder sum() method in Java with Examples

Last Updated : 28 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.sum() is an inbuilt method in java that returns the sum.

Syntax:

public long sum()

Parameters: The function does not accepts any parameter.

Return value: The method returns the current sum.

Program below demonstrates the function:

Below programs demonstrate the above function:

Program 1:




// Java program to demonstrate
// the LongAdder.sum() method
  
import java.lang.*;
import java.util.concurrent.atomic.LongAdder;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initialized with 0
        LongAdder num = new LongAdder();
  
        // Print the initial value
        System.out.println("Initial value is: "
                           + num);
  
        // Add 6 to it
        num.add(6);
  
        // Print the final value
        System.out.println("After addition"
                           + " of 6, value is: "
                           + num);
  
        // Add 5 to it
        num.add(5);
  
        // Print the final value
        System.out.println("After addition"
                           + " of 5, value is: "
                           + num);
  
        // sum operation on num
        num.sum();
  
        // Print after sum
        System.out.println("Returned sum value is: "
                           + num);
    }
}


Output:

Initial value is: 0
After addition of 6, value is: 6
After addition of 5, value is: 11
Returned sum value is: 11

Program 2:




// Java program to demonstrate
// the LongAdder.sum() method
  
import java.lang.*;
import java.util.concurrent.atomic.LongAdder;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Initialized with 0
        LongAdder num = new LongAdder();
  
        // Print the initial value
        System.out.println("Initial value is: "
                           + num);
  
        // Add 10 to it
        num.add(10);
  
        // Print the final value
        System.out.println("After addition"
                           + " of 10, value is: "
                           + num);
  
        // Add 100 to it
        num.add(100);
  
        // Print the final value
        System.out.println("After addition"
                           + " of 100, value is: "
                           + num);
  
        // sum operation on num
        num.sum();
  
        // Print after sum
        System.out.println("Returned sum value is: "
                           + num);
    }
}


Output:

Initial value is: 0
After addition of 10, value is: 10
After addition of 100, value is: 110
Returned sum value is: 110


Similar Reads

LongAdder add() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.add() is an inbuilt method in java that adds the given value. Syntax: public void add(long x) Parameters: The function accepts a single parameter x that specifies the value to be added. Return value: The method do not returns any value. Below programs demons
2 min read
LongAdder increment() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.increment() is an inbuilt method in java that increases the value by 1. Syntax: public void increment() Parameters: The function does not accepts any parameter. Return value: The method do not returns any value. Below programs demonstrate the above function:
2 min read
LongAdder decrement() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.decrement() is an inbuilt method in java that reduces the value by 1. Syntax: public void decrement() Parameters: The function does not accepts any parameter. Return value: The method do not returns any value. Below programs demonstrate the above function: P
2 min read
LongAdder longValue() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.longValue() is an inbuilt method in java that returns the sum(). This method is equivalent to the sum() method. When the object of the class is created its initial value is zero. Syntax: public long longValue() Parameters: The function does not accepts any p
2 min read
LongAdder floatValue() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.floatValue() is an inbuilt method in java that returns the sum as a float value. When the object of the class is created its initial value is zero. Syntax: public float floatValue() Parameters: The function does not accepts any parameter. Return value: The m
2 min read
LongAdder reset() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.reset() is an inbuilt method in Java that resets the sum to zero. When the object of the class is created its initial value is zero. Syntax: public void reset() Parameters: The function does not accepts any parameter. Return Value: This method does not retur
2 min read
LongAdder toString() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.toString() is an inbuilt method in Java that returns the String representation of this LongAdder. When the object of the class is created its initial value is zero. Syntax: public String toString() Parameters: The function does not accepts any parameter. Ret
2 min read
LongAdder intValue() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.intValue() is an inbuilt method in java that returns the sum as a int value. When the object of the class is created its initial value is zero. Syntax: public int intValue() Parameters: The function does not accepts any parameter. Return value: The method re
2 min read
LongAdder doubleValue() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.doubleValue() is an inbuilt method in java that returns the sum as a double value. When the object of the class is created its initial value is zero. Syntax: public double doubleValue() Parameters: The function does not accepts any parameter. Return value: T
2 min read
LongAdder sumThenReset() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.sumThenReset() is an inbuilt method in Java that is equivalent to sum() then reset() function. Firstly, the sum is calculated and then it is reset to the value 0. When the object of the class is created its initial value is zero. Syntax: public long sumThenR
2 min read
Practice Tags :