- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math.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.
Example 1
In this example, we're showing the usage of Math.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 Math.max() method we're printing the maximum value of the given doubles.
public class Test { public static void main(String args[]) { double d1 = 100.675; double d2 = 100.500; System.out.println(Math.max(d1,d2)); } }
This will produce the following result &maxus;
Output
100.675
Example 2
In this example, we're showing the usage of Math.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 Math.max() method we're printing the maximum value of the given floats.
public class Test { public static void main(String args[]) { float d1 = (float)100.675; float d2 = (float)100.500; System.out.println(Math.max(d1,d2)); } }
This will produce the following result &maxus;
Output
100.675
Example 3
In this example, we're showing the usage of Math.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 Math.max() method we're printing the maximum value of the given ints.
public class Test { public static void main(String args[]) { int d1 = 101; int d2 = 100; System.out.println(Math.max(d1,d2)); } }
This will produce the following result &maxus;
Output
101
Example 4
In this example, we're showing the usage of Math.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 Math.max() method we're printing the maximum value of the given ints.
public class Test { public static void main(String args[]) { long d1 = 101L; long d2 = 100L; System.out.println(Math.max(d1,d2)); } }
This will produce the following result &maxus;
Output
101
To Continue Learning Please Login
Login with Google