๐ŸŒ
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:
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Math.html
Math (Java Platform SE 8 )
October 20, 2025 - 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 ...
Discussions

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
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
math - Find the max of 3 numbers in Java with different data types - Stack Overflow
Say I have the following three constants: final static int MY_INT1 = 25; final static int MY_INT2 = -10; final static double MY_DOUBLE1 = 15.5; I want to take the three of them and use Math.max() to More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ max function in java
Max Function in Java - Scaler Topics
April 20, 2024 - We will discuss few examples to understand max() method in Java. The simple example finds maximum value between two integers. ... We get 45 because the maximum of 4 and 45 is 45. Let's understand how to get a maximum value from an array using Math.max() method.
๐ŸŒ
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: ...
Find elsewhere
๐ŸŒ
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!
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ Math โ€บ max
Java Math max() - Find Maximum Value | Vultr Docs
December 3, 2024 - The Math.max() function in Java offers a straightforward way to find the maximum value between two numbers. Ideal for applications across a broad spectrum of domains, this method enhances code efficiency and readability by abstracting away the ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Math โ€บ max
Math.max() - JavaScript | MDN
The Math.max() static method returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-max-method
Java Math max() Method - GeeksforGeeks
May 14, 2025 - Example 1: In this example, we will see the basic usage of Math.max() method with Double values. ... // Java program to demonstrate // the use of max() function // when two double data-type // numbers are passed as arguments import java.lang.Math; public class Geeks { public static void main(String args[]) { double a = 12.123; double b = 12.456; // prints the maximum of two numbers System.out.println(Math.max(a, b)); } }
๐ŸŒ
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.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ โ€บ apcsareview โ€บ VariableBasics โ€บ minAndMax.html
3.7. Integer Min and Max โ€” AP CSA Java Review - Obsolete
What do the last two lines print out? Did this surprise you? Java will actually return the maximum integer value if you try to subtract one from the minimum value. This is called underflow. And, Java will return the minimum integer value if you try to add one to the maximum.