Introduction to Java max() Function
The java max() function used to return the maximum of two numerical values. The java max() function is a built in function in java, which is define in Java.lang.math class, so to use the max() function in a program the Java.lang.math class must to be import. The java max() function accepts two parameters of numerical data types and return maximum among the both numerical parameters, it can be overloaded for different parameters of data types like int, float, double and long. It does not throw any exception.
Syntax of Java max()
Following is a syntax:
Syntax :
data_Type max(data_Type x, data_Type y)
The data_types can be int, float, double and long.
Below are the parameters of Java max() function:
The x and y are two numerical parameters among which maximum number is to be return.
Return value: The return value of the function is max of two numbers, which is same data_type as that of the parameters pass.
The max() function in java can be overloaded for different parameters of data types so following are the syntax of max() function in java:
publicstatic int max(int x, int y);
publicstatic float max(float x, float y) ;
publicstatic long max( long x, long y );
publicstatic double max(double x, double y);
Examples to Implement Java max() Function
Below are the examples of Java max() function:
Example #1
we write the java code to understand the max() function more clearly with the following example where we use max() function to find the maximum of the two integer number passed by user, as below.
Code:
import java.lang.Math;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x = 1024, y = 2034, result;
// calling max() built in function which is define in Math built in class
result = Math.max(x,y);
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
4.5 (143 ratings)
View Course
Output:
Explanation:
As in the above code the x and y are two variables declare and initialize with 1024 and 2034 values respectively, later, the max() function is called with passing these variables as parameters (int max(int x, int y) overloaded)and hence the result of this function is 2034, which is maximum number in 1024 and 2034.
Example #2
We write the java code to understand the max() function where we use max() function to find the maximum of the two double number passed by user, as below.
Code:
import java.lang.Math;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
double x = 134.78, y = 56.89, result;
// calling max() built in function which is define in Math built in class
result = Math.max(x,y);
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Output:
Explanation:
As in the above code the x and y are two variables declare and initialize with 134.78 and 56.89 double values respectively, later, the max() function is called with passing these variables as parameters (double max(double x, double y) overloaded)and hence the result of this function is 134.78, which is maximum number in 1024 and 2034.
Example #3
We write the java code to understand the max() function where we use max() function to find the maximum of the two integer number which are accepted by user from console, as below.
Code:
import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
// accept two numbers of integer type from console
System.out.println( "Enter the Two Numeric value: ");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
result = Math.max(x,y);
//Print the maximum number between x and y
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Output:
As in the above code the x and y are two variables declare and accepted these variable values from the user through scanner object. Later, the max() function is called with passing these variables as parameters and hence the result of this function based on the user entered values as 10 and 20 is 20.
Example #4
We write the java code for max() function where we use max() function to pass two negative values and find the maximum, as below.
Code:
import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
x = -10;
y = -20;
result = Math.max(x,y);
//Print the maximum number between x and y of lower magnitude
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Output:
As in the above code the x and y are two variables declare and initialize with negative values -10 and -20 values respectively, later, the max() function is called with passing these variables as parameters and hence the result of this function is -10 which lower magnitude.
Example #5
We write the java code for max() function where we use max() function to pass positive and negative values and find the maximum, as below.
Code:
import java.lang.Math;
import java.util.Scanner;
class Demo
{
public static void main( String[] arg )
{
// variable declaration
int x, y, result;
x = 10;
y = -20;
result = Math.max(x,y);
//Print the maximum number between x and y
System.out.println("The maximum number among "+x+" and "+y+" is "+result+"." );
}
}
Output :
As in the above code the x and y are two variables declare and initialize with values 10 and -20 values respectively, and the result of this function is 10.
Conclusion
The java max() function which is a built in function in Java.lang.math class is used to to gets the maximum of two numerical values as we have seen above with example.
Recommended Articles
This is a guide to Java max(). Here we discuss the Introduction to Java max() function and its parameters along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –