×

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type.

Syntax:

  1. public static double max(double a, double b)
  2. public static float max(float a, float b)
  3. public static int max(int a, int b)
  4. public static long max(long a, long b)

Parameters:

The parameters ‘a’ and ‘b’ represent the two values.

Return Value:

The max() method returns the larger of a and b.

  • It returns the result of same value, is the arguments have same value
  • It returns NaN, if either value is NaN.
  • It returns positive zero, if one argument is positive zero and the other is negative zero.

Example 1:

public class JavaMathMaxExample1 {
    public static void main(String[] args) {
        double a = 20;
        double b=134;
        //returns the greater of two numbers
        System.out.println("The greater number between "+a+" and "+ b+ " is
:
"+Math.max(a,b));
    }
}

Output:

The greater number between 20.0 and 134.0 is : 134.0

Example 2:

public class JavaMathMaxExample2 {
    public static void main(String[] args) {
        int a = 20;
        int b= Integer.MAX_VALUE;
        //returns the greater of two numbers
        System.out.println("The greater number between "+a+" and "+ b+ "
is : "+Math.max(a,b));
    }
}

Output:

The greater number between 20 and 2147483647 is : 2147483647

Example 3:

public class JavaMathMaxExample3 {
    public static void main(String[] args) {
        Double a = 20.9d;
        Double b=Double.NaN;
        //returns NaN, if either value is NaN
        System.out.println("The greater number between "+a+" and "+ b+ "
is : "+Math.max(a,b));
    }
}

Output:

The greater number between 20.9 and NaN is : NaN

Example 4:

public class JavaMathMaxExample4 {
    public static void main(String[] args) {
        float a = 0f;
        float b=-0f;
        //returns positive zero, if one argument is positive zero and the
other is negative zero
        System.out.println("The greater number between "+a+" and "+ b+ "
is : "+Math.max(a,b));
    }
}

Output:

The greater number between 0.0 and -0.0 is : 0.0

Example 5:

public class JavaMathMaxExample5 {
    public static void main(String[] args) {
        float a =Float.MIN_VALUE ;
        double b=Double.MIN_VALUE;
        System.out.println("The greater number between "+a+" and "+ b+ "
is : "+Math.max(a,b));
    }
}

Output:

The greater number between 1.4E-45 and 4.9E-324 is : 1.401298464324817E-45

Related Topics

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Java Rename File

Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible. A file can be renamed using Java's renameTo()...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.