×

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

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 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.

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.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.