Java max() Method: Usage and Examples
Table of Content:
Description
On this, tutorial we will be showing a java example on how to use the max() method of Math Class. The max() returns which of the two method argument has the highest value numerically. This method is overloaded such that it handles all primitive data type. Here are the overloaded methods:
This method gives the maximum of the two arguments. The argument can be int, float, long, double.
Syntax
This method has the following variants ?
double max(double arg1, double arg2) float max(float arg1, float arg2) int max(int arg1, int arg2) long max(long arg1, long arg2)
The overloaded methods are basically the same, it’s just that they deal with different data type either int, long, float, and double
Most of the methods of the Math class is static and the max() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.max(a).
Parameters
Here is the detail of parameters ?
- This method accepts any primitive data type as a parameter.
Return Value
- This method returns the maximum of the two arguments.
Example
public class MathMaxMethod { public static void main(String args[]) { System.out.println(Math.max(22.123, 21.456)); System.out.println(Math.max(23.12, 23.0)); } }
Output
22.123 23.12 Press any key to continue . . .
Java Math max() Example
import java.util.Scanner; /* * This example source code demonstrates the use of * max() method of Math class */ public class MathMaxExample { public static void main(String[] args) { // Ask for user input System.out.print("Enter first value:"); // use scanner to read the console input Scanner scan = new Scanner(System.in); // Assign the 1st input to String variable String value1 = scan.nextLine(); // ask for the second input System.out.print("Enter second value:"); // Assign the 2nd input to String variable String value2 = scan.nextLine(); // close the scanner object scan.close(); // convert the values to int int a = Integer.parseInt(value1); int b = Integer.parseInt(value2); // get the result of max method int result = Math.max(a,b); System.out.println("Result of the operation:"+result); } }
Output
Enter first value:12 Enter second value:45 Result of the operation:45 Press any key to continue . . .
Find maximum of two numbers using Math.max
/* Find maximum of two numbers using Math.max This java example shows how to find maximum of two int, float, double or long numbers using max method of Java Math class. */ public class FindMaxOfTwoNumbersExample { public static void main(String[] args) { /* * To find maximum of two int values, use * static int max(int a, int b) method of Math class. */ System.out.println(Math.max(20,40)); /* * To find minimum of two float values, use * static float max(float f1, float f2) method of Math class. */ System.out.println(Math.max(324.34f,432.324f)); /* * To find maximum of two double values, use * static double max(double d2, double d2) method of Math class. */ System.out.println(Math.max(65.34,123.45)); /* * To find maximum of two long values, use * static long max(long l1, long l2) method of Math class. */ System.out.println(Math.max(435l,523l)); } }
Output
40 432.324 123.45 523 Press any key to continue . . .