Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Float sum() Method

Java sum() method is a part of the Float class of the java.lang package. This method returns the numerical sum of the float values passed as arguments (i.e simply adds the two numbers passed as argument in accordance with the + operator).

Syntax:

public static float sum(float a, float b)  

Parameters:

The parameters passed includes the two float values whose addition is to be returned.

Returns:

Returns the sum of the two float values passed as parameters.

Example 1:

Here, some positive and negative numbers are taken for a better understanding of the method.

import java.lang.Float;

public class StudyTonight
{  
    public static void main(String[] args) 
    {          
        float a = 10.84f;  
        float b = 29.132f;  
        float c = -67.99f;

        System.out.println("The sum of a and b is = " + Float.sum(a, b));  // It will return the sum of a and b
        System.out.println("The sum of a and b is = " + Float.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.857998

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);  
            float a = sc.nextFloat();  
            float b = sc.nextFloat();  
            System.out.println("sum is = " + Float.sum(a, b));  // will return the sum of a and b. 
        }
        catch(Exception e)
        {
            System.out.println("Invalid Input");
        }  
    }  
}  


Enter the two values :
89.56 42.789
sum is = 132.349
***************************
Enter the two values :
-56.94 89.43
sum is = 32.49
***************************
Enter the two values :
90 0x598
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.