Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

Java Absolute Maximum: Why Equal Magnitudes Need a Tie-Breaker

A Java absolute-maximum example chooses the greater signed value when magnitudes tie, making results independent of input order. But Math.abs(int) has an important Integer.MIN_VALUE boundary.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When two Java integers have the same absolute magnitude, the result still needs a tie policy. In Mauricio Ramirez’s DEV Community article about an absolute-maximum exercise, the method chooses the greater signed value on a tie—so it returns 5 for both (-5, 5) and (5, -5). That detail makes the result independent of input order. The example also has a boundary caveat: Java’s Math.abs does not produce a positive result for Integer.MIN_VALUE.

What does “absolute maximum” mean here?

The exercise’s goal is to select the input integer with the greatest absolute magnitude. That is not the same as selecting the numerically largest signed integer: for example, -10 has a greater absolute magnitude than 5.

As an Amazon Associate I earn from qualifying purchases.

Ramirez’s example uses a varargs method, AbsoluteMax.getMaxValue(int... numbers). It starts by selecting the first number, then scans the remaining values. A candidate replaces the current selection if its absolute magnitude is larger, or if the magnitudes are equal and the candidate’s signed value is greater.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why compare the signed values when magnitudes tie?

Opposite-sign values can have equal absolute magnitudes. For -5 and 5, both magnitudes are 5, so magnitude alone cannot decide which value to return. The second comparison supplies the rule: return the greater signed value. Thus 5 wins whether the input is (-5, 5) or (5, -5).

Without that comparison, a scan that keeps the current value on a magnitude tie would return whichever tied value appeared first. The output would then depend on input order rather than the method’s stated preference. A tie-breaker makes that behavior explicit.

How the example handles inputs

The article’s examples cover mixed-sign values, all-negative values, a single value, repeated equal-magnitude cases, and an empty varargs call. A single value is already the only possible result; with an empty input there is no first value to select.

The method checks for a null or empty array and throws IllegalArgumentException in either case. In Java, a call with no arguments to a varargs method supplies an empty array, so the no-argument example exercises the empty-input guard.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the example misses: Integer.MIN_VALUE

The tie-break lesson is sound, but the displayed use of Math.abs(int) does not correctly rank every possible Java int by mathematical magnitude. Oracle’s Java SE 21 documentation explains that Math.abs(Integer.MIN_VALUE) returns Integer.MIN_VALUE, still negative, because the positive magnitude cannot fit in an int. See the Java SE 21 Math.abs(int) documentation.

That creates a concrete failure for the displayed comparison: because the computed “absolute value” of Integer.MIN_VALUE is negative, a routine comparing these results as ordinary integers can treat it as smaller than other magnitudes. Tests that use values such as -5 and 5 do not expose this boundary.

Choose the boundary policy before changing the code

If the contract is to rank by mathematical magnitude across the full int range, convert each value to long before taking its absolute value. A long can represent the positive magnitude of every Java int, including Integer.MIN_VALUE. The method can then use the same signed-value tie-break after comparing widened magnitudes.

If instead the contract treats an unrepresentable absolute value as an error, Java SE 21 provides Math.absExact(int), which throws ArithmeticException when the result overflows. That is an overflow policy, not a complete absolute-maximum algorithm: the method still needs to define how such an exception is handled and how ties are decided. See the Math.absExact(int) documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

The broader lesson from the edge case

Ramirez describes the experience personally: “Coding on "automatic mode" for a long time made me overlook these details.” The example shows why it helps to make the method’s contract precise: specify what “maximum” measures, how ties are resolved, which inputs are rejected, and what happens at numeric boundaries. A few ordinary examples can demonstrate the intended tie rule, but boundary values are needed to establish whether the implementation fulfills the contract across Java’s full integer range.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.