Java - Number max() Method



Description

The method gives the smaller of the two arguments. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other negative zero, the result is positive zero. The argument can be int, float, long, double.

Syntax

This method has the following variants &maxus;

public static double max(double arg1, double arg2)
public static float max(float arg1, float arg2)
public static int max(int arg1, int arg2)
public static long max(long arg1, long arg2)

Parameters

Here is the detail of parameters &maxus;

  • This method accepts any primitive data type as a parameter.

Return Value

  • This method returns the smaller of the two arguments.

Getting Maximum of Two double Values Example

In this example, we're showing the usage of Double.max() method to get the maximum of two double values. We've created two double variables d1, d2 and initialized them with different values. Then using Double.max() method we're printing the maximum value of the given doubles.

package com.tutorialspoint;

public class Test { 
   public static void main(String args[]) {
      double d1 = 100.675;
      double d2 = 100.500;
     
      System.out.println(Double.max(d1,d2));	  
   }
}

This will produce the following result &maxus;

Output

100.675

Getting Maximum of Two float Values Example

In this example, we're showing the usage of Float.max() method to get the maximum of two float values. We've created two float variables d1, d2 and initialized them with different values. Then using Float.max() method we're printing the maximum value of the given floats.

package com.tutorialspoint;

public class Test { 
   public static void main(String args[]) {
      float d1 = (float)100.675;
      float d2 = (float)100.500;
     
      System.out.println(Float.max(d1,d2));	  
   }
}

This will produce the following result &maxus;

Output

100.675

Getting Maximum of Two int Values Example

In this example, we're showing the usage of Integer.max() method to get the maximum of two int values. We've created two int variables d1, d2 and initialized them with different values. Then using Integer.max() method we're printing the maximum value of the given ints.

package com.tutorialspoint;

public class Test { 
   public static void main(String args[]) {
      int d1 = 101;
      int d2 = 100;
     
      System.out.println(Integer.max(d1,d2));	  
   }
}

This will produce the following result &maxus;

Output

101

Getting Maximum of Two long Values Example

In this example, we're showing the usage of Long.max() method to get the maximum of two long values. We've created two long variables d1, d2 and initialized them with different values. Then using Long.max() method we're printing the maximum value of the given longs.

package com.tutorialspoint;

public class Test { 
   public static void main(String args[]) {
      long d1 = 101L;
      long d2 = 100L;
     
      System.out.println(Long.max(d1,d2));	  
   }
}

This will produce the following result &maxus;

Output

101
java_numbers.htm
Advertisements