Java Math max() Function

max Function

Java Math Class

Here, in the page we will discuss about the max() Function in java.
Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), copySign() etc.The java.lang.Math class contains various methods for performing basic numeric operations

Java max Function :

Java Collection contains Math class and The class Math contains methods for performing several basic and advance numeric operations such as the exponential, logarithm, square root, and trigonometric functions.
The max Function takes two arguments, say num1 and num2, and returns the maximum between two of them. The arguments are can be of  int, double, float and long type.

Syntax:

Math.max(number1,number2);

Definition of Parameters:

Return Type :

Example :

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    int num1 = 8;
    int num2 = 10;
    
    // Printing Maximum between 2 Inputs
    System.out.println("Maximum of two Elements is : " + Math.max(num1,num2));
  }
}

Output:

Maximum of two Elements is : 10

Explanation:

In the above Example, We had taken two Integer values. The max Function returns the maximum between two of the inputs.

Example for Different Data Values:

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {

    // Integer arguments
    int num1 = 3;
    int num2 = 1;
    System.out.println("Maximum in Integers Values : " + Math.max(num1, num2));

    // Long arguments
    long num3 = 47831L;
    long num4 = 852224L;
    System.out.println("Maximum in Long Values : " + Math.max(num3, num4));

    // Float arguments
    float num5 = 7.6f;
    float num6 = 2.8f;
    System.out.println("Maximum in Float Values : " + Math.max(num5, num6));

    // Double arguments
    double num7 = 13.4d;
    double num8 = 21.01d;
    System.out.println("Maximum in Double Values : " + Math.max(num7, num8));
  }
}

Output:

Maximum in Integers Values : 3
Maximum in Long Values : 852224
Maximum in Float Values : 7.6
Maximum in Double Values : 21.01

Explanation:

In the above Example, We had taken two arguments of Integer, Long, Float and double values. The max function returns the maximum value between two of the arguments.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription