🌐
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: ...
🌐
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.
Discussions

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
calculating the max using math import - Stack Overflow
im starter in java so i would be happy if somebody answer my question in a simple way. im trying to calculate max by using max import :s but it gives me error :( here is the script : package Cal... 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
How to use "Math.min" and "Math.max"?
Did you even look at the API Docs? http://docs.oracle.com/javase/6/docs/api/ Math.min requires two arguments, not 3. When in doubt, look at the function you are calling and see what it does, otherwise you're just "guessing" which leads to bad habits. bholzer's code should suffice. The next question would be, do you know why his code fixes the problem? More on reddit.com
🌐 r/learnprogramming
7
12
September 3, 2012
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - If the first argument is zero, then a zero of the same sign is returned. ... Returns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. See the Java Language Specification for a discussion of floating-point value sets.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java math.max() method
Java Math.max() Method
September 1, 2008 - HR Interview Questions · Computer Glossary · Who is Who · Previous · Quiz · Next · 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.
🌐
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.
🌐
JavaBeat
javabeat.net › home › how to use the math.max() function in java?
How to Use the Math.max() Function in Java?
January 23, 2024 - In Java, the Math.max() function accepts two numbers as arguments, performs comparison, and retrieves the maximum number.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.math.max
Math.Max Method (Java.Lang) | Microsoft Learn
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. Returns the greater of two float values.
🌐
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 ...
Find elsewhere
🌐
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
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › math
Max numbers with Math.max in Java - Examples Java Code Geeks - 2025
May 5, 2021 - In the second example below, we will be using Math.max()with positive and negative values. ... This was an example of how to find the maximum numbers in Java, using the Math class and the method Math.max().
🌐
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.
🌐
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.
🌐
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!
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 17633625 › calculating-the-max-using-math-import
calculating the max using math import - Stack Overflow
im starter in java so i would be happy if somebody answer my question in a simple way. im trying to calculate max by using max import :s but it gives me error :( ... package Calc; import java.lang.Math.*; public class Calculator { public static void main(String[] args) { int n1,n2; String s="10 , 20"; //converting strings to integers String s1=s.substring(3,s.length()); String [] s2=s1.split(","); n1=Integer.parseInt(s2[0]); n2=Integer.parseInt(s2[1]); //max int maxstock = Math.max(n1 , n2); System.out.println(maxstock); } }
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java math max() function
Java Math max() Function » PREP INSTA
May 30, 2023 - import java.util.*; public class Main{ public static void main(String[] args) { int num1 = 8; int num2 = 10; // Printing Maximum between 2 Inputs System.out.println("Maximum of two Elements is : " + Math.max(num1,num2)); } }
🌐
Tutorial Gateway
tutorialgateway.org › java-max-function
Java max Function
March 28, 2025 - In this example, we use the Math.max function to find the maximum value from different data types and display the output. In this example, First, We used the Function directly on both the Positive int or integer and negative integer values. The first three systems out println statements will call the integer type ( static int max(integer a, integer b) ) method and find the largest among the values. Here, we used the Java Math.max Function on variables b, c, d, and e (they belong to double type).
🌐
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 - Yes, Math.max and Math.min in a loop saving the result through each iteration. Once the loop is done, you have the max and/or min. — masijade 1,351 Jump to Post · 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.