BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.max() Method

By Chaitanya Singh | Filed Under: java

Java Math.max() returns the greater number between two passed numbers. In this tutorial, we will discuss this method in detail with the help of examples.

public class JavaExample
{
  public static void main(String args[])
  {
    float num = -15.5f;
    float num2 = -5.55f;
    //This will print the greater float number
    System.out.println(Math.max(num, num2));

    double d1 = 1001.22;
    double d2 = 2500.33;
    //This will print the greater double number
    System.out.println(Math.max(d1, d2));
  }
}

Output:

-5.55
2500.33

Syntax of Math.max() Method:

System.out.println(Math.max(101, 102)); //print 102

max() Description:

public static int max(int a, int b): Compares the passed int numbers and returns the greater integer number. This method has total four variations in the Math class to compare different data types such as float, long and double. These variations are:

public static int max(int a, int b);
public static long max(long a, long b);
public static float max(float a, float b);
public static double max(double a, double b);

max() Parameters

This method takes two parameters:

  • a: The first argument.
  • b: The second argument.

max() Return Values

  • Returns the greater number between a and b.
  • If one argument is positive and other is negative then max() returns positive argument.
  • If the arguments are NaN (Not a Number) then NaN is returned.
  • If both the arguments are negative then the argument with lower absolute value is returned. For example: -15 and -5, the max number is -5.

Example 1: Max of two int numbers

public class JavaExample
{
  public static void main(String args[])
  {
    int a = 10;
    int b = 20;
    int x = -10;
    int y = -20;
    System.out.println("Max of a and b: "+Math.max(a,b));
    System.out.println("Max of x and y: "+Math.max(x,y));
  }
}

Output:

Java Math.max() Example Output_1

Example 2: Max of two float numbers

public class JavaExample
{
  public static void main(String args[])
  {
    float a = 5.5f, b = 20.56f;
    float x = -5.5f, y = -20.56f;
    System.out.println("Max of a and b: "+Math.max(a,b));
    System.out.println("Max of x and y: "+Math.max(x,y));
  }
}

Output:

Java Math.max() Example Output_2

Example 3: Max of two double numbers

public class JavaExample
{
  public static void main(String args[])
  {
    double a = 5000, b = 8000;
    double x = -5000, y = -8000;
    System.out.println("Max of a and b: "+Math.max(a,b));
    System.out.println("Max of x and y: "+Math.max(x,y));
  }
}

Output:

Java Math.max() Example Output_3

Example 4: Math.Max() Method with NaN

public class JavaExample
{
  public static void main(String args[])
  {
    double x =0.0/0; //NaN (Not a Number)
    System.out.println("Max of 10 and NaN: "+Math.max(10, x));
  }
}

Output:

Java Math.max() Example Output_4
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap