Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).
Top answer 1 of 12
110
Math.max only takes two arguments. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)).
2 of 12
31
you can use this:
Collections.max(Arrays.asList(1,2,3,4));
or create a function
public static int max(Integer... vals) {
return Collections.max(Arrays.asList(vals));
}
CodeGym
codegym.cc › java blog › java math › java math max() method
Java Math.max() method with Examples
December 5, 2024 - This Math.max() method can only take two arguments, so you can’t use it to find a Maximum number in a set with more than two numbers. It has four overloading methods for int, double, float, and long data types. Here are the method signatures of 4 methods. public static int max(int a, int b) public static double max(double a, double b) public static long max(long a, long b) public static float max(float a, float b)
Videos
What is the purpose of the Math.max() function in Java?
The primary purpose of Math.max() is as a programming tool that discovers the biggest number between any two input numbers using an effective mathematical detection method.
upgrad.com
upgrad.com › home › tutorials › software & tech › the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
Does Math.max() work with negative numbers?
Yes, the Math.max() function in JavaScript verifies negative numbers throughout its execution to provide the highest value from all arguments regardless of their sign.
upgrad.com
upgrad.com › home › tutorials › software & tech › the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
Can Math.max() be used to compare more than two numbers?
The original goal of Math.max() in Java and JavaScript matches only two numbers yet programmers can obtain a multiple number maximum through sequential function execution or alternative solution approaches.
upgrad.com
upgrad.com › home › tutorials › software & tech › the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
W3Schools
w3schools.com › java › ref_math_max.asp
Java Math max() Method
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
DaniWeb
daniweb.com › programming › software-development › threads › 172908 › max-and-min-of-more-than-2-numbers
java - Max and Min of more than 2 numbers | DaniWeb
I just started programming in Java and i dont think i would know how to program a method to make it detect the min/max in an array. is there any method to just use the Math.min/Math.max for trying to see which one is the largest/smallest from the users input? ... Besides, if you simply want to "take the easy way out", place your numbers in an array (which I assume they are already, anyway), and use Arrays.sort() and then pull the first and last numbers.
Tutorialspoint
tutorialspoint.com › java › lang › math_max_int.htm
Java - Math max(int x, int y) Method
The following example shows the usage of Math max() method of a positive and a negative value. package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two int numbers int x = -60984; int y = 497; // call max and print the result System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y)); } }
Upgrad
upgrad.com › home › tutorials › software & tech › the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
March 18, 2025 - Java allows you to find maximum values between multiple numbers by connecting multiple Math.max() operations sequentially.
Codecademy
codecademy.com › docs › java › math methods › .max()
Java | Math Methods | .max() | Codecademy
October 22, 2022 - Beginner Friendly.Beginner Friendly17 hours17 hours ... The .max() method takes two parameters num1 and num2. num1 and num2 can be of type int, float, double or long. The following example uses the .max() method to print the maximum of two numbers:
Scaler
scaler.com › home › topics › max function in java
Max Function in Java - Scaler Topics
April 20, 2024 - The first answer is you compare both real numbers using comparison operators like greater-than (>) or less-than (<) operators and find the maximum number. The second method is to take the help of the inbuilt max() method of Math class in Java ...
Reddit
reddit.com › r/javahelp › can you use the math.max() method for 4 values instead of 2?
r/javahelp on Reddit: Can you use the Math.max() method for 4 values instead of 2?
November 14, 2021 -
I need to compare 4 values and determine which one is biggest.
Top answer 1 of 3
38
Two things: Change the variables x, y, z as int and call the method as Math.max(Math.max(x,y),z) as it accepts two parameters only.
In Summary, change below:
String x = keyboard.nextLine();
String y = keyboard.nextLine();
String z = keyboard.nextLine();
int max = Math.max(x,y,z);
to
int x = keyboard.nextInt();
int y = keyboard.nextInt();
int z = keyboard.nextInt();
int max = Math.max(Math.max(x,y),z);
2 of 3
2
You should know more about java.lang.Math.max:
java.lang.Math.max(arg1,arg2)only accepts 2 arguments but you are writing 3 arguments in your code.- The 2 arguments should be
double,int,longandfloatbut your are writingStringarguments in Math.max function. You need to parse them in the required type.
You code will produce compile time error because of above mismatches.
Try following updated code, that will solve your purpose:
import java.lang.Math;
import java.util.Scanner;
public class max {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input 3 integers: ");
int x = Integer.parseInt(keyboard.nextLine());
int y = Integer.parseInt(keyboard.nextLine());
int z = Integer.parseInt(keyboard.nextLine());
int max = Math.max(x,y);
if(max>y){ //suppose x is max then compare x with z to find max number
max = Math.max(x,z);
}
else{ //if y is max then compare y with z to find max number
max = Math.max(y,z);
}
System.out.println("The max of three is: " + max);
}
}
Javatpoint
javatpoint.com › java-math-max-method
Java Math.max() method with Examples - Javatpoint
Java CopyOnWriteArrayList · indexOf() lastIndexOf() clone() toArray() Math.abs() Math.max() Math.min() Math.round() Math.sqrt() Math.cbrt() Math.pow() Math.signum() Math.ceil() Math.copySign() Math.nextAfter() Math.nextUp() Math.nextDown() Math.floor() Math.floorDiv() Math.random() Math.rint() Math.hypot() Math.ulp() Math.getExponent() Math.IEEEremainder() Math.addExact() Math.subtractExact() Math.multiplyExact() Math.incrementExact() Math.decrementExact() Math.negateExact() Math.toIntExact() Math.log() Math.log10() Math.log1p() Math.exp() Math.expm1() Math.sin() Math.cos() Math.tan() Math.asin() Math.acos() Math.atan() Math.sinh() Math.cosh() Math.tanh() Math.toDegrees ·
Naukri
naukri.com › code360 › library › math-max-in-java
Math.max in Java - Naukri Code 360
Almost there... just a few more seconds
Top answer 1 of 2
9
You could use varargs:
Copypublic static Integer max(Integer... vals) {
Integer ret = null;
for (Integer val : vals) {
if (ret == null || (val != null && val > ret)) {
ret = val;
}
}
return ret;
}
public static void main(String args[]) {
System.out.println(max(1, 2, 3, 4, 0, -1));
}
Alternatively:
Copypublic static int max(int first, int... rest) {
int ret = first;
for (int val : rest) {
ret = Math.max(ret, val);
}
return ret;
}
2 of 2
1
You can use a simple loop:
Copypublic Integer max(final Collection<Integer> ints) {
Integer max = Integer.MIN_VALUE;
for (Integer integer : ints) {
max = Math.max(max, integer);
}
return max;
}
TutorialKart
tutorialkart.com › java › java-math › java-math-max
Java Math.max() - Maximum Value
November 23, 2020 - max() accepts two numbers as arguments, and returns the greater of the two values.
Tutorialspoint
tutorialspoint.com › home › java/lang › java math.max() method
Understanding Java Math.max() Method
September 1, 2008 - Following is the declaration for java.lang.Math.max() method ... This method returns the larger of a and b. ... The following example shows the usage of Math max() method of two positive values. package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two double numbers double x = 60984.1; double y = 497.99; // call max and print the result System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y)); } }
TutorialsPoint
tutorialspoint.com › get-the-maximum-of-two-numbers-using-math-max-in-java
Get the maximum of two numbers using Math.max in Java
Let us see a program to get the maximum of two numbers using the Math.max() method ... import java.lang.Math; public class Example { public static void main(String[] args) { // declaring and intializing some integer values int a = 10; int b = 9; // declaring and intializing some float values ...