×

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

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

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

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Virtual Function in Java

In the Operated Oriented Programming language, a virtual function or virtual method is a collection of functions that overrides the functionality of a function in an inheriting class with the same...

4 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.