Yes an no. You can call Math.max(...) multiple times however. int max = Math.max(a,b); max = Math.max(max, c); ... Answer from MarSara on reddit.com
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
3 days ago - Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. See the Java Language Specification for a discussion of floating-point value sets. If the exponent of the result is between Double.MIN_EXPONENT and ...
🌐
W3Schools
w3schools.com › java › ref_math_max.asp
Java Math max() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate · ❮ Math Methods · Get the highest value from different pairs of numbers: ...
Discussions

Can you use the Math.max() method for 4 values instead of 2?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
6
1
November 14, 2021
java - How to use Math.min and Math.max in integer array - Stack Overflow
I want to find the maximum and minimum values in an integer array but I'm unable to use them . Eclipse throws this error The method min(int, int) in the type Math is not applicable for the More on stackoverflow.com
🌐 stackoverflow.com
How does the Math.max function work in this code?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
6
1
September 11, 2022
Is Math.max(a,b) or (a>b)?a:b faster in Java? - Stack Overflow
Which one is faster in Java and why? Math.max(a,b) (a>b)?a:b (This was asked in an interview.) More on stackoverflow.com
🌐 stackoverflow.com
People also ask

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
Are there alternative methods to find the maximum value without using Math.max()?
Performing a search through all values while holding a variable that stores the biggest value seen until then enables you to find the maximum without using Math.max().
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-max-method
Java Math max() Method - GeeksforGeeks
May 14, 2025 - The max() method in Java is a part of java.lang.Math class. This is an inbuilt function in Java that returns maximum of two numbers.
🌐
Educative
educative.io › answers › what-is-mathmax-in-java
What is Math.MAX() in JAVA?
The MAX() function returns the largest value from the two numbers sent as a parameter · Figure 1 shows a visual representation of the MAX() function
🌐
CodeGym
codegym.cc › java blog › java math › java math max() method
Java Math.max() method with Examples
December 5, 2024 - public class Main { public static void main(String args[]) { int x = 40; int y = 60; int z = 75; //Find the maximum among three values using max() function System.out.println(Math.max(z, Math.max(x,y))); } } The output will be 75. The function max() is a simple method in Java that’s very easy to use.
Find elsewhere
🌐
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 - This approach streamlines the process of evaluating two numbers and identifying the greater one, which is a frequent action in various programming contexts, including statistical analysis, game creation, or any situation where determining the maximum value is essential. Java includes the Math.max() method, which belongs to the java.lang.Math class, for finding the maximum of two values, offering a straightforward and efficient means to identify the larger of two numbers.
🌐
Programiz
programiz.com › java-programming › library › math › max
Java Math max()
Become a certified Java programmer. Try Programiz PRO! ... The max() method returns the maximum value among the specified arguments. class Main { public static void main(String[] args) { // compute max of 88 and 98 System.out.println(Math.max(88, 98)); } } // Output: 98
🌐
iO Flood
ioflood.com › blog › math-max-java
Math.max() Java Function | Usage, Syntax, and Examples
February 29, 2024 - This is a quick and concise way to find the maximum of two numbers, though it may be less readable than Math.max() for those unfamiliar with the ternary operator. If you have an array of numbers and want to find the maximum, one approach is to sort the array in ascending order and pick the last element. Here’s how you can do this in Java:
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java math.max() method
Understanding Java Math.max() Method
September 1, 2008 - The Java Math max(double a, double b) returns the greater of two double values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value.
🌐
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
February 2, 2009 - Math.min/max only compare two values at a time, so to handle N values you iterate once, carrying the current min and max. Initialize both to the first value to correctly handle all-negative inputs and to avoid guessing sentinel extremes.
🌐
Reddit
reddit.com › r/javahelp › how does the math.max function work in this code?
r/javahelp on Reddit: How does the Math.max function work in this code?
September 11, 2022 -
class consecutiveOnes
{

public int findMaxConsecutiveOnes(int[] nums) { //constructor that accepts as an argument an int array called nums.

        int maxHere = 0, max = 0; //int variable declarations initialized with zero.
        for (int n : nums) //for each loop. for int n in nums array:
            max = Math.max(max, maxHere = n == 0 ? 0 : maxHere +1); //ternary operator. max is assigned the math.max function (different use of max). max is the total max values consecutively. maxHere is the max value per n in the array.
                                                                    // This says if the larger of the two values (max, maxHere)

                                                                    // is zero, return zero and overwrite max with zero(this ensures the consecutive counter for 1 is reset), else increment maxHere by 1.
        return max;     //returns max
    }

    public static void main (String[] args)
    {
        consecutiveOnes co = new consecutiveOnes();

        int nums[] = {1, 0, 1, 1, 1, 1};

        //int n = nums.length;

        System.out.println("number of consecutive ones are: " +co.findMaxConsecutiveOnes(nums));

    }
}

I understand that the Math.max function returns the highest of two values (in this case, the highest of max and maxHere). Included are comments of what I believe is happening. What I don't understand is why the Math.max function is not returning 2 for max when for example, n in the array is 1. Would it not be the case since maxHere is incrementing by 1?

My rationalization is if n = 1, and maxhere +1 or increments by 1, then maxHere = 2 and new max value is 2, but that would not make sense, especially because consecutive count would be 2 at the first run of the loop...

EDIT: I apologize for the confusion. The code runs fine, but I have a limited understanding of why the Math.max function is choosing maxHere = 1 for max and not maxHere = 2 when maxHere = n =1 since maxHere + 1 would execute.

Top answer
1 of 4
2
As stated your code works fine. I'm a little confused as to what you're confused about, but if you step through the loop then you will have the following results; maxHere = 1, max = 0; -> max = 1; maxHere = 0, max = 1; -> max = 1; maxHere = 1, max = 1; -> max = 1; maxHere = 2, max = 1; -> max = 2; maxHere = 3, max = 2; -> max = 3; maxHere = 4, max =3; -> max = 4; And then after the loop it returns max which is 4.
2 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Quora
quora.com › Can-we-use-the-min-and-max-methods-in-Java-for-getting-the-minimum-and-maximum-among-more-than-two-numbers
Can we use the min. and max. methods in Java for getting the minimum and maximum among more than two numbers? - Quora
Answer: If you are asking about min and max method of java.lang.Math class then No. These method can only compare min or max between two number only. This is the method signature; max(int a, int b) min(int a, int b) You can clearly see above it takes two arguments only hence can compare two v...
🌐
Codecademy
codecademy.com › docs › java › math methods › .max()
Java | Math Methods | .max() | Codecademy
October 22, 2022 - The Math.max() method returns the maximum value from the given two arguments. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
Website Files
cdn.prod.website-files.com › 6804cf62eab9bf4963afd1a2 › 68659796664d8f56484bc02d_56542246945.pdf pdf
Math.max in java
=================================== Java provides an inbuilt function called max() in its java.lang.Math class. This method is used to return the maximum of two numbers.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_max_int.htm
Java - Math max(int x, int y) Method
The java.lang.Math.max(int a, int b) returns the greater of two int values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value.
🌐
Scaler
scaler.com › home › topics › max function in java
Max Function in Java - Scaler Topics
April 20, 2024 - The max() method is an inbuilt method of Math class which is present in java.lang package that is used to find the maximum of two numbers.