• Home
  • How to import math functions in Java?

How to import math functions in Java?

In order to use the functions in the java.lang.Math class in your Java program, you will need to import the class. This can be done using the import statement at the beginning of your program:

import java.lang.Math;

Once you have imported the Math class, you can access its functions using the following syntax: Math.functionName(). For example, to use the sqrt() function, which calculates the square root of a number, you can write the following code:

double squareRoot = Math.sqrt(4.0); // squareRoot will be 2.0

You can find a list of all the functions available in the Math class in the Java documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html.