I would simplify this by introducing a variable max:

public static int max(int a, int b, int c, int d) {

    int max = a;

    if (b > max)
        max = b;
    if (c > max)
        max = c;
    if (d > max)
        max = d;

     return max;
}

You could also use Math.max, as suggested by fast snail, but since this seems to be homework, I would prefer the algorithmic solution.

Math.max(Math.max(a,b),Math.max(c,d))
Answer from Patrick Hofman on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_math_min.asp
Java Math min() 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 lowest value from different pairs of numbers: ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-min-method-examples
Java Math min() method with Examples - GeeksforGeeks
January 9, 2026 - The value with the greater negative magnitude is considered smaller. Hence, -25 is returned as it is less than -23. If Math.min() is used frequently, you can statically import the method to avoid repeated class qualification. ... import static java.lang.Math.min; class GFG { public static void main(String[] args) { int a = 3; int b = 4; System.out.println(min(a, b)); } }
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 does math.min actually work? - Stack Overflow
I understand that all of the math functions in java are built in. But I was wondering out of curiosity how Math.min() actually works? I checked the java documentation and couldn't find anything to... More on stackoverflow.com
🌐 stackoverflow.com
Finding the minimum of 4 numbers, NOT using an array? - Oracle Forums
I wan't to be able to do something like this Math.min(w,x,y,z) so i can find the smallest value of 4 numbers. More on forums.oracle.com
🌐 forums.oracle.com
October 9, 2008
range - Java - limit number between min and max - Stack Overflow
I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min and Math.max. public int limit(int More on stackoverflow.com
🌐 stackoverflow.com
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › min
Java Math min() - Get Minimum Value | Vultr Docs
September 27, 2024 - Even if the numbers are the same, Math.min() effectively handles this by returning the value itself, as both are the minimum.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_min_int.htm
Java - Math min(int x, int y) Method
The Java Math min(int a, int b) returns the smaller of two int values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value.
🌐
Quora
quora.com › Can-Math-min-have-only-two-parameters-in-Java
Can Math.min have only two parameters in Java? - Quora
Answer (1 of 2): Sometimes I don’t know wether the guys asking the questions here are lazy, stupid, trolls or just plain blank. Look at Math (Java Platform SE 7 ) You will find that Math.min() has 4 implementations, each only hold 2 arguments. If ...
Find elsewhere
🌐
Scaler
scaler.com › home › topics › min() in java
min() in Java - Scaler Topics
April 8, 2024 - The outcome will be the same if ... in java can be of the following types: int( for ex: 2,6,81), double (for ex: 22.4, 3.5), float( for ex: 2.65f, 7.89f), and long(for ex: 10l, 2l)....
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-min-method
Java Math.min() Method
October 9, 2022 - public static int min(int a, int b): Compares the passed integer numbers and returns the smaller integer number. This method has total four variations in the Math class, to compare different data types such as float, long and double.
🌐
Programiz
programiz.com › java-programming › library › math › min
Java Math min()
returns the minimum value among the specified arguments · class Main { public static void main(String[] args) { // Math.min() with int arguments int num1 = 35; int num2 = 88; System.out.println(Math.min(num1, num2)); // 35 // Math.min() with long arguments long num3 = 64532L; long num4 = 252324L; System.out.println(Math.min(num3, num4)); // 64532 // Math.min() with float arguments float num5 = 4.5f; float num6 = 9.67f; System.out.println(Math.min(num5, num6)); // 4.5 // Math.min() with double arguments double num7 = 23.44d; double num8 = 32.11d; System.out.println(Math.min(num7, num8)); // 23.44 } } In the above example, we have used the Math.min() method with int, long, float, and double type arguments.
Top answer
1 of 6
22

int

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

long

public static long min(long a, long b) {
     return (a <= b) ? a : b;
}

float

public static float min(float a, float b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0f) && (b == 0.0f) && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
         return b;
    }
    return (a <= b) ? a : b;
 }

double

public static double min(double a, double b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0d) && (b == 0.0d) && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
        return b;
    }
    return (a <= b) ? a : b;
}

More info: Here

2 of 6
10

Java 7 documentation:

Returns the smaller of two int values. That is, the result the argument closer to the value of Integer.MIN_VALUE. If the arguments have the same value, the result is that same value.

Behaviour:

Math.min(1, 2) => 1
Math.min(1F, 2) => 1F
Math.min(3D, 2F) => 2D
Math.min(-0F, 0F) => -0F
Math.min(0D, -0D) => -0D
Math.min(Float.NaN, -2) => Float.NaN
Math.min(-2F, Double.NaN) => Double.NaN

java.lang.Math and java.lang.StrictMath Source:

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

java.lang.Math Bytecode (javap -c Math.class of Oracle's JDK's JRE's rt.jar):

public static int min(int, int);
Code:
   0: iload_0           // loads a onto the stack
   1: iload_1           // loads b onto the stack
   2: if_icmpgt     9   // pops two ints (a, b) from the stack
                        // and compares them
                        // if b>a, the jvm continues at label 9
                        // else, at the next instruction, 5
                        // icmpgt is for integer-compare-greater-than
   5: iload_0           // loads a onto the stack
   6: goto          10  // jumps to label 10
   9: iload_1           // loads 
  10: ireturn           // returns the currently loaded integer

If the comparison at 5 is true, a will be loaded, the jvm will jump to 10 and return a, if the comparison yields false, it will jump to 9, which will load and return b.

Intrinsics:

This .hpp file of the Java 8 Hotspot JVM hints that it optimizes Math.min even further with optimized machine code:

do_intrinsic(_min, java_lang_Math, min_name, int2_int_signature, F_S)

This means the above bytecode won't be executed by the Java 8 Hotspot JVM. However, this differs from JVM to JVM, which is why I also explained the bytecode!

Hopefully, now you know all there is to know about Math.min! :)

🌐
Codecademy
codecademy.com › docs › java › math methods › .min()
Java | Math Methods | .min() | Codecademy
October 22, 2022 - The Math.min() method returns the minimum value from the given two arguments. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
🌐
Oracle
forums.oracle.com › ords › apexds › post › finding-the-minimum-of-4-numbers-not-using-an-array-0116
Finding the minimum of 4 numbers, NOT using an array? - Oracle Forums
October 9, 2008 - I wan't to be able to do something like this Math.min(w,x,y,z) so i can find the smallest value of 4 numbers.
🌐
Tutorial Gateway
tutorialgateway.org › java-min-function
Java min Function
March 22, 2025 - In this example, we use the Math.min function to find the Minimum or Smaller value from a different set of data types and display the output. First, we used it directly on both positive and negative integer values. The first three statements will call the integer type static int min(integer a, integer b) method and find the smallest among the values. Next, We used the Java Math.min Function on variables b, c, d, and e (they belong to double type).
🌐
Javatpoint
javatpoint.com › java-math-min-method
Java Math.min() method with Examples - Javatpoint
November 23, 2020 - Java Math.min() method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
🌐
TutorialKart
tutorialkart.com › java › java-math › java-math-min
Java Math.min() - Minimum Value
September 22, 2023 - If you provide arguments of different datatype, then the lower datatype is promoted to higher datatype, and smallest value with the respective datatype is returned. ... public class MathExample { public static void main(String[] args) { int a = 5; float b = 10.2F; float result = Math.min(a, b); System.out.println(result); } } ... In this example, among int and float, float is higher datatype and hence, a is promoted to float. min(int, float) becomes min(float, float). In this Java Tutorial, we learned about Java Math.min() function, with example programs.
🌐
iO Flood
ioflood.com › blog › math-min-java
Using Math.min in Java: A Detailed Tutorial
February 29, 2024 - Check for NaN values: Before passing values to Math.min(), ensure they’re valid numbers. If there’s a chance of encountering NaN values, add checks to handle them. Remember that negative numbers are smaller than positive numbers: When dealing with negative numbers, remember that they’re considered smaller than all positive numbers. Ensure consistent data types: When comparing numbers of different data types, Java will automatically convert the smaller data type to the larger one, which can lead to unexpected results.
🌐
JavaBeat
javabeat.net › home › how to use math.min() method in java?
How to Use Math.min() Method in Java?
November 30, 2023 - To use the “Math.min()” method in Java, pass two variables from which the minimum value needs to be found as arguments and the method returns the smallest value argument. If one of the operand values is negative, then the negative value will be returned and it can be worked with all primitive data types like int, float, double, and long.