LAST UPDATED: OCTOBER 15, 2020
Java Double sum() Method
Java sum()
method is a part of the Double
class of the java.lang
package. This method returns the numerical sum of the double values passed as arguments (i.e simply adds the two numbers passed as argument in accordance with the + operator).
Syntax:
public static double sum(double a, double b)
Parameters:
The parameters passed includes the two double values whose addition is to be returned.
Returns:
Returns the sum of the two double values passed as parameters.
Example 1:
Here, some positive and negative numbers are taken for a better understanding of the method.
import java.lang.Double;
public class StudyTonight
{
public static void main(String[] args)
{
double a = 10.84;
double b = 29.132;
double c = -67.99;
System.out.println("The sum of a and b is = " + Double.sum(a, b)); // It will return the sum of a and b
System.out.println("The sum of a and b is = " + Double.sum(b, c)); // It will return the sum of a and b
}
}
The sum of a and b is = 39.972
The sum of a and b is = -38.85799999999999
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the two values : ");
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.println("sum is = " + Double.sum(a, b)); // will return the sum of a and b.
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
Enter the two values :
89.66 95.88
sum is = 185.54
***************************
Enter the two values :
-66.009 -443.99
sum is = -509.999
**************************
Enter the two values :
0x667 0x554
Invalid Input
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.