.max()
Published Oct 22, 2022
Contribute to Docs
The Math.max()
method returns the maximum value from the given two arguments.
Syntax
Math.max(num1, num2)
- The
.max()
method takes two parametersnum1
andnum2
. num1
andnum2
can be of typeint
,float
,double
orlong
.
Example 1
The following example uses the .max()
method to print the maximum of two numbers:
// Main.javapublic class Main {public static void main(String[] args){int num1 = 50;int num2 = 10;System.out.println(Math.max(num1, num2));}}
This results in the following output:
50
Example 2
The following example uses the .max()
method to print the maximum of three numbers:
public class Main {public static void main(String[] args){int num1 = 4;int num2 = 10;int num3 = 1;System.out.println(Math.max(Math.max(num1, num2),num3));}}
This results in the following output:
10
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours