×

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

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

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.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.