Join our newsletter for the latest updates.

Java Math hypot()

The Java Math hypot() method computes the square root of x2 + y2 (i.e. hypotenuse) and returns it.

The syntax of the hypot() method is:

Math.hypot(double x, double y)

Note: The hypot() method is a static method. Hence, we can call the method directly using the class name Math.


hypot() Parameters

  • x, y - double type arguments

hypot() Return Values

  • returns Math.sqrt(x2 + y2)

The returned value should be within the range of the double data type.

Note: The Math.sqrt() method returns the square root of specified arguments. To learn more, visit Java Math.sqrt().


Example 1: Java Math.hypot()

class Main {
  public static void main(String[] args) {

    // create variables
    double x = 4.0;
    double y = 3.0;

    //compute Math.hypot()
    System.out.println(Math.hypot(x, y));  // 5.0

  }
}

Example 2: Pythagoras Theorem Using Math.hypot()

class Main {
  public static void main(String[] args) {

    // sides of triangle
    double  side1 = 6.0;
    double side2 = 8.0;

    // According to Pythagoras Theorem
    // hypotenuse = (side1)2 + (side2)2
    double hypotenuse1 = (side1) *(side1) + (side2) * (side2);
    System.out.println(Math.sqrt(hypotenuse1));    // prints 10.0

    // Compute Hypotenuse using Math.hypot()
    // Math.hypot() gives √((side1)2 + (side2)2)
    double hypotenuse2 = Math.hypot(side1, side2);
    System.out.println(hypotenuse2);               // prints 10.0

  }
}

In the above example, we have used the Math.hypot() method and the Pythagoras Theorem to calculate the hypotenuse of a triangle.