Library contents

Java Math.hypot

In Java, Math.hypot computes the length of a right triangle’s hypotenuse from its two legs. Legs of 3 and 4 give a length of 5.

A simple example

Math.hypot

  1. Start with Legs 3 and 4
  2. Do this Combine perpendicular lengths
  3. Result 5
The squared lengths sum to 25. Its square root is 5.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.hypot(3.0, 4.0));
    }
}
Output
5.0

Syntax

Java syntax
Math.hypot(width, height)
JDK 21 method signatures (1)
public static double hypot(double x, double y)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Measure the diagonal of a screen

A designer knows the horizontal and vertical distances between two points. Calculate the direct diagonal distance so the measurement can be used without tracing two separate legs.

From the programdouble width = 3.0
Result5.000
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        double width = 3.0;
        double height = 4.0;

        System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.hypot(width, height)));
    }
}
Output
5.000

A screen three units wide and four high has a five-unit diagonal.

Advertisements

Estimate a direct walk across campus

A student crosses a courtyard instead of following two perpendicular paths around it. Calculate the direct walking distance from the horizontal and vertical distances shown on the campus map.

From the programdouble east = 6.0
Result10.000
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        double east = 6.0;
        double north = 8.0;

        System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.hypot(east, north)));
    }
}
Output
10.000

Six blocks east and eight north form perpendicular sides. Their direct diagonal measures ten blocks.

Measure an exhibit’s diagonal cable route

An exhibition plan shows a socket 5 m east and 12 m north of a screen. A drafter marks the straight-line separation on the plan.

GivenEast 5 m · north 12 m
Worked result13 m

The perpendicular offsets form the legs of a right triangle.

What to watch for

Both inputs must use the same unit. A diagonal is a length, not an area.

Documentation for Math.hypot