Home » Java programming language

Java Math Class static int max(int i1,int i2) with example

Java Math Class static int max(int i1,int i2) method: Here, we are going to learn about the static int max(int i1,int i2) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 05, 2019

Math Class static int max(int i1,int i2)

  • This method is available in java.lang package.
  • This method is used to return the maximum one of both the given arguments in the method or in other words it returns the largest value of the given two arguments.
  • This is a static method so it is accessible with the class name too.
  • The return type of this method is int, it returns the largest element from the given two arguments (which are of int types).
  • In this method, we pass two integers (int) parameters as an argument.
  • This method does not throw any exception.

Syntax:

    public static int max(int i1, int i2){
    }

Parameter(s): int i1, int i2 – two integer values, in which we have to find the largest value.

Return value:

The return type of this method is int, it returns the largest/maximum value.

Note:

  • If we pass "NaN", it returns "NaN".
  • If we pass the same value as both parameters, it returns the same value.

Java program to demonstrate example of max(int i1,int i2) method

// Java program to demonstrate the example of 
// max(int i1, int i2) method of Math Class

public class MaxIntTypeMethod {
    public static void main(String[] args) {
        // declaring variables
        int i1 = -0;
        int i2 = 0;
        int i3 = -2;
        int i4 = 124;

        // displaying the values
        System.out.println("i1: " + i1);
        System.out.println("i2: " + i2);
        System.out.println("i3: " + i3);
        System.out.println("i4: " + i4);

        // Here , we will get (0) because we are 
        // passing parameter whose value is (-0,0)
        System.out.println("Math.max(i1,i2): " + Math.max(i1, i2));

        // Here , we will get (124) and we are 
        // passing parameter whose value is (0,124)
        System.out.println("Math.max(i2,i4): " + Math.max(i2, i4));
    }
}

Output

E:\Programs>javac MaxIntTypeMethod.java

E:\Programs>java MaxIntTypeMethod
i1: 0
i2: 0
i3: -2
i4: 124
Math.max(i1,i2): 0
Math.max(i2,i4): 124


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.