java.lang.Float sum(float a, float b)

Description

The sum(float a, float b) method of Float class is very simple, it just returned the sum of the two floating point method argument a and b. Basically this is just another way of having the operation a + b.

Method Syntax

public static float sum(float a, float b)

Method Argument

Data Type Parameter Description
float a the first operand
float b the second operand

Method Returns

The sum(float a, float b) method of Float class returns float which corresponds to the sum of a and b.

Compatibility

Java 1.8

Java Float sum(float a, float b) Example

Below is a simple java example on the usage of sum(float a, float b) method of Float class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * sum(float a, float b) method of Float class.
 */

public class FloatSumExample {

	public static void main(String[] args) {

		// Ask user input for first number
		System.out.print("Enter First Number:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get value from user console
		Float f1 = scan.nextFloat();
		// Ask user input for second number
		System.out.print("Enter Second Number:");
		Float f2 = scan.nextFloat();
		// close the scanner object
		scan.close();
		
		// get the sum of the two float input
		Float result = Float.sum(f1, f2);
		System.out.println("The sum is: "+result);		
	}
}

Basically on the above example, we just ask for two float numbers from the console and we used the scanner class to get these values assigned as Float object. We then get the sum of the float input.

Sample Output

Below is the sample output when you run the above example.

Float sum() Sample Output