๐ŸŒ
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
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
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 โ€บ java โ€บ lang โ€บ math_max_float.htm
Java - Math max(float x, float y) Method
The Java Math max(float a, float b) returns the greater of two float 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. If either value is NaN, then the result is NaN. Unlike the numerical comparison ...