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
🌐
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[]) { double x = 15.68; double y = -37.47; System.out.println(Math.max(x, y)); } } The output will be 15.68 Finding the maximum value between two floating-point numbers. public class Main { public static void main(String args[]) { float x = -21.44f; float y = -23.32f; System.out.println(Math.max(x, y)); } } The output will be -21.44f Finally, let’s find the maximum value between the two long values.
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
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
Can Math.max() be used to compare more than two numbers?
The original goal of Math.max() in Java and JavaScript matches only two numbers yet programmers can obtain a multiple number maximum through sequential function execution or alternative solution approaches.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
🌐
W3Schools
w3schools.com › java › ref_math_max.asp
Java Math max() Method
Tip: Use the min() method to return the number with the lowest value. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Codecademy
codecademy.com › docs › java › math methods › .max()
Java | Math Methods | .max() | Codecademy
October 22, 2022 - // Main.java · public class Main ... output: 50 · Copy to clipboard · Copy to clipboard · The following example uses the .max() method to print the maximum of three numbers: public class Main { public static void main(String[] ...
🌐
Scaler
scaler.com › home › topics › max function in java
Max Function in Java - Scaler Topics
April 20, 2024 - Let's understand the max() in Java with the help of examples. 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.
Find elsewhere
🌐
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
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. is there any method to just use the Math.min/Math.max for trying to see which one is the largest/smallest from the users input? ... Besides, if you simply want to "take the easy way out", place your numbers in an array (which I assume they are already, anyway), and use Arrays.sort() and then pull the first and last numbers.
🌐
iO Flood
ioflood.com › blog › math-max-java
Math.max() Java Function | Usage, Syntax, and Examples
February 29, 2024 - In this example, we use the Math.max() function to find the maximum of the numbers 5 and 10. The function returns 10, which is the larger of the two numbers, and this value is then printed to the console. This is a basic way to use Math.max in Java, but there’s much more to learn about this function and its applications.
🌐
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 - We utilize the Math.max() method two times to assess the three numbers: Initially, it determines the higher value between num1 and num2. Next, it evaluates the outcome against num3 to determine the overall maximum. Ultimately, we display the maximum value. ... Enter the first number: 10 Enter the second number: 20 Enter the third number: 15 The maximum number is: 20 · Here’s a Java function that accepts an integer array as input and produces the maximum value through a loop:
🌐
Javatpoint
javatpoint.com › java-math-max-method
Java Math.max() method with Examples - Javatpoint
Java Math.max() method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
🌐
Blogger
java-demos.blogspot.com › 2013 › 10 › find-maximum-and-minimum-of-numbers.html
Find Maximum and Minimum of numbers — Java Demos
// Math.max(a,b) gives maximum ... } } If you want the maximum for 4 numbers, then you would write, int max=Math.max(Math.max(Math.max(a,b),c),d); But isn't it ridiculous to make these many method calls, when we can do ...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › max
Java Math max() - Find Maximum Value | Vultr Docs
December 3, 2024 - In this snippet, Math.max() evaluates x and y, and returns x because it holds the higher value.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-max-method
Java Math max() Method - GeeksforGeeks
May 14, 2025 - Example 2: In this example, we will see the usage of Math.max() method with integer values including both positive and negative number. ... // Java program to demonstrate // the use of max() function // when one positive and one // negative integer are passed // as argument import java.lang.Math; public class Geeks { public static void main(String args[]) { int a = 23; int b = -23; // prints the maximum of two numbers System.out.println(Math.max(a, b)); } }
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_max_int.htm
Java - Math max(int x, int y) Method
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two int numbers int x = -60984; int y = -497; // call max and print the result System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y)); } } Let us compile and run the above program, this will produce the following result − · Math.max(-60984,-497)=-497 · java_lang_math.htm ·
🌐
Programiz
programiz.com › java-programming › library › math › max
Java Math max()
In the above example, we have used the Math.max() method with int, long, float, and double type arguments. class Main { public static void main(String[] args) { // create an array of int type int[] arr = {4, 2, 5, 3, 6}; // assign first element of array as maximum value int max = arr[0]; for (int i = 1; i < arr.length; i++) {
🌐
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, arrays are the collection of related data. The example given below demonstrates the functionality of the Math.max() method with an integer array: public class FindMaxValue { public static void main(String[] args) { int[] numArray= {5,6,4,2,3,7,1,9}; int Maximum=numArray[0]; for (int i=0;i<numArray.length;i++) { Maximum=Math.max(Maximum, numArray[i]); } System.out.println("The maximum value in the array is " + Maximum); } }
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-max-method
Java Math.max() Method
public class JavaExample { public static void main(String args[]) { double a = 5000, b = 8000; double x = -5000, y = -8000; System.out.println("Max of a and b: "+Math.max(a,b)); System.out.println("Max of x and y: "+Math.max(x,y)); } } ... public class JavaExample { public static void main(String args[]) { double x =0.0/0; //NaN (Not a Number) System.out.println("Max of 10 and NaN: "+Math.max(10, x)); } }
🌐
Linus Tech Tips
linustechtips.com › software › programming
Math.max(x,y) comparing 4 integers (Java) - Programming - Linus Tech Tips
November 16, 2015 - Hi. I made a very simple game in Java where there are 4 players, and they receive points for various tasks. Now I want to check who got the most points, and figured I could use the Math.max() method. However, it only works for 2 integers, and I am very unsure about how it actually works. What I h...