Find Biggest of Three Numbers in Java

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Conditionals

What you’ll learn

  • How to pick the largest of three integers with a clear if–else if–else ladder.
  • An equivalent nested max style using a tiny max2 helper (same result, different shape).
  • Why >= matters when values can tie, plus a browser live preview.

Overview

Given three integers, return the one that is not smaller than the other two. When two or three values tie for largest, any of the tied values is a correct “biggest”; the programs on this page return one of them deterministically.

Two programs

Classic if-ladder plus a max2 composition (or nested Math.max).

Live preview

Type three integers and see the maximum immediately.

Interview polish

Mention ties, extension to N values, and O(1) cost for three fixed slots.

Prerequisites

Comfort with if, else if, else, and relational operators >, >=.

  • public static void main(String[] args), integer variables, and printing with System.out.println.
  • Optional: logical && to combine two comparisons in one condition.

What does “biggest of three” mean?

For integers a, b, and c, the maximum (biggest) is any value m in {a,b,c} such that m ≥ a, m ≥ b, and m ≥ c. If the largest value appears more than once, those inequalities still hold.

Using >= makes ties behave predictably: the first matching branch returns a tied winner instead of falling through by accident.

If-ladder explicit branches
Nested max max(a,b) then vs c
General N loop + running max

Formal note

Binary max is associative for totally ordered types: max(a, b, c) = max(max(a, b), c). That identity is the correctness argument behind the two-argument helper version (or nested Math.max).

Example

max(14, 7, 22) = 22 because 22 is greater than or equal to both 14 and 7.

Intuition and examples

Picture three poles of different heights: scan from left to right, remembering the tallest seen so far. For exactly three fixed slots, you can hard-code the same comparisons.

14, 7, 22 Max 22
Check
22 ≥ 14 and 22 ≥ 7
Verdict
Matches the sample program output.
5, 5, 3 Max 5
Tie
Two values share the maximum.
Verdict
>= branches return 5 (first branch wins).
9, 9, 9 Max 9
Triple tie
Every value is largest.
Verdict
First branch returns 9.

Takeaway: this is total-order comparison, not sorting the whole list.

Live preview

Enter three integers. The widget uses the same ordering as JavaScript Math.max on three numbers (handles ties naturally).

Whole numbers match the Java samples; the preview still runs if you type decimals.

Live result
Press “Run” to see the biggest value.

Algorithm

Goal: return the largest of three comparable values a, b, c.

Test whether a wins

If a >= b and a >= c, then a is a maximum; return it.

Else test whether b wins

If b >= a and b >= c, return b.

Otherwise return c

If neither a nor b dominates both others, c must.

📜 Pseudocode

Pseudocode
function findBiggest(a, b, c):
    if a >= b and a >= c:
        return a
    if b >= a and b >= c:
        return b
    return c
1

If–else if ladder (reference style)

Classic interview solution: three mutually exclusive branches with && and >=.

java
public class Main {
    static int findBiggest(int num1, int num2, int num3) {
        if (num1 >= num2 && num1 >= num3) {
            return num1;
        } else if (num2 >= num1 && num2 >= num3) {
            return num2;
        }
        return num3;
    }

    public static void main(String[] args) {
        int number1 = 14;
        int number2 = 7;
        int number3 = 22;
        int result = findBiggest(number1, number2, number3);

        System.out.println("The biggest number is: " + result);
    }
}

Explanation

Each branch names one candidate and proves it beats (or ties) the other two.

num1 >= num2 && num1 >= num3

Candidate num1. Both comparisons must hold so num1 is not smaller than either rival.

else if (num2 >= num1 && num2 >= num3)

Only if the first test failed. Then ask the same style of question for num2.

return num3;

Last case. No third if is needed: if num1 and num2 are not maximal, num3 is.

2

Nested two-argument maximum

Same semantics, compact form. Easy to generalize mentally to a loop for longer lists.

java
public class Main {
    static int max2(int a, int b) {
        return (a > b) ? a : b;
    }

    static int findBiggest3(int a, int b, int c) {
        return max2(max2(a, b), c);
        // or: return Math.max(Math.max(a, b), c);
    }

    public static void main(String[] args) {
        int number1 = 14;
        int number2 = 7;
        int number3 = 22;
        int result = findBiggest3(number1, number2, number3);

        System.out.println("The biggest number is: " + result);
    }
}

Explanation

max2 uses strict >; ties pick the left operand, which is fine because any tied largest is acceptable.

max2(max2(a, b), c)

Associative fold. First reduce the first pair, then compare the winner against c.

Optimization

Three values only. Any correct solution is already O(1); micro-optimizations are rarely asked.

Larger N. Read into an array and track best in one pass instead of hand-unrolling comparisons.

Floating types. For double values, use Math.max but remember NaN rules if you are dealing with invalid data.

Interview: write the if-ladder first for clarity; offer max2 nesting as a follow-up refactor.

❓ FAQ

Compare pairs with if-else, or compute max(a,b) once and then max of that result with c. Both return the largest value; using >= instead of > keeps behavior correct when two or all three numbers are equal.
If you only use strict >, two equal largest values can fall through incorrectly depending on branch order. Chaining >= against both other operands picks a winner whenever the current candidate is tied for largest.
Yes. Java provides Math.max(x, y) for primitive numeric types. You can nest it: Math.max(Math.max(a, b), c).
Extend the same idea: loop with a running maximum, or fold nested max calls. Loops scale better for large N.
A fixed number of comparisons for three values is O(1) time and O(1) extra space.
Comparing ints does not add them, so max-of-three does not overflow. Beware only if later computations involve sums or products.

🔄 Input / output examples

Both sample programs print one line. Swap literals or read interactive inputs using Scanner.

Inputs (a, b, c)Printed line
14, 7, 22The biggest number is: 22
5, 5, 3The biggest number is: 5
-1, -4, -2The biggest number is: -1

Edge cases and pitfalls

Watch branch order when you use only strict >, and be explicit about ties if the problem wants a specific tie-break rule.

Ties

Two or three equal maxima

The if-ladder with >= returns the first candidate that can tie-win. That is usually acceptable; if the spec needs a specific tie break, mention it and adjust.

Negatives

All values below zero

Comparisons still work: the “biggest” is the least negative (closest to zero).

Helpers

Prefer Math.max or a method

In Java, methods have no macro side effects. You can use Math.max or write your own max2 to keep it readable.

⏱️ Time and space complexity

ApproachTimeExtra space
If-ladder or nested max for three intsO(1)O(1)
Max of N numbers (single pass)O(N)O(1)

Both programs on this page use only a few integer locals: auxiliary space is constant.

Summary

  • Task: return the maximum of three integers (ties allowed).
  • Patterns: if-ladder with >= and &&, or max2(max2(a,b),c) / nested Math.max.
  • Watch-outs: tie semantics with strict > only, and scaling to N with a loop.
Did you know?

Comparing three values takes at most three pairwise checks in the obvious if-ladder form. The same answer can be built by nesting max(a,b) twice — both styles are O(1) time and O(1) space.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful