Java Math hypot()

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The hypot() method of java.lang.Math class returns the square root of x2+y2x^2 + y^2. The method does not produce an intermediate overflow or underflow.

  • Infinity is the outcome if either of the arguments are infinite.
  • The outcome is NaN if any of the arguments are NaN and neither argument is infinite.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of Math hypot() in Java

The syntax of hypot() method is

Parameters of Math hypot() in Java

Math.hypot() takes two arguments:

  • x, y - double type arguments used to calculate the output as Math.sqrt($x^2 + y^2$).

Return Value of Math hypot() in Java

Math.hypot() returns Math.sqrt(x2x^{2}+y2y^{2}) of double type without intermediate overflow or underflow.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Exceptions of Math hypot() in Java

Math.hypot() does NOT throw any exception.

Example

In this example, we are going to see how to use hypot() method in java.

Output:

Explanation:

  • In the above example, we have initialized x as 3 and y as 4.
  • So as we discussed hypot() method computes square root of x2 + y2 which is equal to 25. Hence, square root of 25 is printed.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

What is Math hypot() in Java

The Java Math.hypot() method calculates the hypotenuse, or square root of x2+y2x^{2} + y^{2}, where x and y are its arguments.

Special cases:

  • The outcome is Infinity if either argument is infinite.
  • The outcome is NaN if any of the arguments is NaN and neither argument is infinite.

More Examples

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1: Pythagoras Theorem Using Math.hypot()

In this example, we are going to find the hypotenuse of the right-angle triangle using Pythagoras' Theorem.

Output:

Explanation:

In the above code, we have used Math.hypot() method to find the hypotenuse of a triangle having height 3 and base 4.

Example 2: Using hypot() with Infinity

In this example we are going to use Math.hypot() with infinity

Output:

Explanation:

As we discussed above that if one of the arguments is infinite, the hypot() method will return Infinity.

Example 3: Using hypot() with One Argument Math.sqrt(-4)

In this Example we are going to use Math.hypot() with one argument as sqrt(-4).

Output:

Explanation:

  • First Math.sqrt(-4) returns NaN as the square root of a negative number.
  • Now arguments passed in hypot() are 3 and NaN, and as discussed this will return NaN.

Example 4: Using hypot() with NaN and Infinity

In this example we are going to use Hypot with NaN and Infinity.

Output:

Explanation:

If any of the arguments is Infinite, the return value is Infinity.

Conclusion

  • hypot() method of Math class return the Euclidean norm, sqrt(xx+yy)sqrt(x * x + y * y).
  • The function does not produce an intermediate overflow or underflow and returns sqrt(x^2 + y^2).
  • Positive Infinity is the outcome if either argument is infinite.
  • The outcome is NaN if any of the arguments is NaN and neither argument is infinite.