QuizCure
Find Available Solution Here!

How to import math class in java

Deepak Jun 11, 2024

Before diving into imports and usage, let's first explore Math class key definitions.

  • Utility class: A collection of static methods to perform basic numeric operations
  • We don’t need to create a Math class object: We can call math class methods directly for example Math.methodName().
  • Math class is part of the java.lang package therefore it's automatically available in every Java program.

Math class Key features:

  • Range of numeric operations methods: Covers basic arithmetic, trigonometry, exponents, logarithms, rounding, etc.
  • Final class: Its method can not be overridden by the child (sub) class.
  • Static methods: The math class method can be called using the class name. No need to create objects

Technically the Math class is part of java.lang package, Therefore it is implicitly imported by default. Here's a clear and concise explanation of how to import the Math class in Java:

Using Fully Qualified Names

Access any method or constant directly using the class name,
Method Math.methodName().
Constant Math.constant

  double squareRoot = java.lang.Math.sqrt(36);
  double piValue = java.lang.Math.PI;

Importing Math class specific method using import Statement

We can access all static members of class Math using static import. Please check the below example for reference

Import specific members of the Math class:

import static java.lang.Math.sqrt;
double squareRoot = sqrt(36);

Import all static members of the Math class

import static java.lang.Math.*;

double squareRoot = sqrt(36);
double piValue = PI;

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up