java.lang.Math – Numerical operations in Java

The java.lang.Math class contains methods for performing basic numerical operations, such as logarithm, square root and trigonometric functions.

What is java.lang.Math in Java?

In the section Mathematical calculations in Java we learned that the most common mathematical operations such as addition, subtraction and multiplication are built into Java by default. But to use more advanced mathematical functions such as the logarithm, raised to and the root of, we need to import a mathematical library, for example java.lang.Math.

Java.lang.Math contains over 75 built-in mathematical operations!

Java has a built-in standard class, java.lang.Math contains several methods to perform standard calculations — the java.lang.Math class facilitates a lot when we are going to perform some mathematical calculations.

  • Java.lang.Math can perform mathematical calculations such as square root, logarithm, or use trigonometric functions such as sine, cosine and tangent, and much more.
  • Java.lang.Math contains over 75 built-in mathematical operations and below are a few examples of how to use some of the methods.
  • For a complete list of operations visit:  Math (Java Platform SE 8 ) – Oracle Help Center

Additionally, we will in chapter 5 take a look at how methods work and how we can create our own methods, but now we are keen on to see how we can use the built-in methods in java.lang.Math to make the different mathematical calculations a lot easier . However, keep in mind that it is important to keep track of what data type (input) is compatible with the calculation you intend to use.

How to use more advanced mathematic operations in Java?

It is easy to use java.lang.Math. At the top of your program, you import the package with the reserved word import, followed by static to make the package visible throughout the program.

import static java.lang.Math*;

You now have access to all the methods in Math. The asterisk (*) means that we import all methods that the package contains, such as pi and Euler’s numbers.

import static java.lang.Math.*;
public class java_math {
    public static void main(String[] args) {

        System.out.println(PI);  // PI: 3.14.. 
        System.out.println(E);   // Eulers tal: 2.71..
    }
}
3.141592653589793
2.718281828459045

However, in this case, we can make it even easier. Since java.lang.Math is a standard library in Java, it is possible to directly call the library without first importing it. To call the library, we simply write Math., followed by the mathematical operation.

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

        System.out.println(Math.PI);  // PI: 3.141..
        System.out.println(Math.E);   // Eulers tal: 2.718..
    }
}

Gives the exact same printout.

Try writing Math. in IntelliJ. Then a long list of all available methods will be displayed!

How to use java.lang.math in Java?

Say we want to calculate 6 ^ 2 (6 “raised to the power of” 2), which is a simple calculation, but the important thing is to see how we can use the methods in a simple way.

double num = Math.pow(6.0, 2.0);  // num = 36.0

Or the square root of 9

double numSquare = Math.sqrt(9.0); // numSquare = 3.0

We use the constants in a similar way. Say we want to calculate 5 ^ Pi (5 “raised to” Pi)

double numPowPi = Math.pow(5.0 , Math.PI);  // numPowPi = 278.377..

Remember, we simply write Math. followed by the method we want to use.

Important notice

As mentioned above, it is important to keep track of what data type we use the method with. For example, we can not write

int num = Math.pow(6, 2);

This will result in a compilation error because the method requires a double. If we want to use an integer we must first make the calculation and save it in a double variable and then convert it into an integer.

Previous Page    |    Next Page