The Javadoc for the Math class provides some information on the differences between the two classes:

Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.

Therefore, the Math class lays out some rules about what certain operations should do, but they do not demand that the exact same results be returned in all implementations of the libraries.

This allows for specific implementations of the libraries to return similiar, but not the exact same result if, for example, the Math.cos class is called. This would allow for platform-specific implementations (such as using x86 floating point and, say, SPARC floating point) which may return different results.

(Refer to the Software Implementations section of the Sine article in Wikipedia for some examples of platform-specific implementations.)

However, with StrictMath, the results returned by different implementations must return the same result. This would be desirable for instances where the reproducibility of results on different platforms are required.

Answer from coobird on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - java.lang.Math · public final class Math extends Object · The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Unlike some of the numeric methods of class StrictMath, all implementations ...
🌐
W3Schools
w3schools.com › java › java_math.asp
Java Math
The Java Math class has many methods that allows you to perform mathematical tasks on numbers.
Discussions

What is the difference between import static java.lang.Math. ...
You don't need to import java.lang.Math, or any part of java.lang. More on stackoverflow.com
🌐 stackoverflow.com
How do I properly use the java.lang.Math? - Stack Overflow
I am in a COMP 110 class which is intro to Java. This specific program is irrelevant to the class but to help my own understanding with Java. I believe I have imported the tool to allow the progra... More on stackoverflow.com
🌐 stackoverflow.com
Is the Math class a standard class of Java? - Stack Overflow
Yes, Math is a standard Java class. Scanner is, too. You have to import it because it lives in the java.util package. ... Every class documented here is considered standard. ... Yes. It is documented as part of the JDK javadoc (since JDK 1.0) so you are guaranteed that it will exist in any JRE you'll ever encounter. Note that since it resides in java.lang... More on stackoverflow.com
🌐 stackoverflow.com
Amazing java.lang.math performance - Performance Tuning - JVM Gaming
Hi all, In researching for our Vector/Matrix classes and our chapter on fast java math, I have written some java.lang.math alternatives, typical techniques from the C/C++ world (look up tables, approximations, even ported a Quake3 inverse sqaure root routine ;-)) However, when attempting to ... More on jvm-gaming.org
🌐 jvm-gaming.org
17
0
September 17, 2003
Top answer
1 of 4
82

The Javadoc for the Math class provides some information on the differences between the two classes:

Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.

Therefore, the Math class lays out some rules about what certain operations should do, but they do not demand that the exact same results be returned in all implementations of the libraries.

This allows for specific implementations of the libraries to return similiar, but not the exact same result if, for example, the Math.cos class is called. This would allow for platform-specific implementations (such as using x86 floating point and, say, SPARC floating point) which may return different results.

(Refer to the Software Implementations section of the Sine article in Wikipedia for some examples of platform-specific implementations.)

However, with StrictMath, the results returned by different implementations must return the same result. This would be desirable for instances where the reproducibility of results on different platforms are required.

2 of 4
30

@ntoskrnl As somebody who is working with JVM internals, I would like to second your opinion that "intrinsics don't necessarily behave the same way as StrictMath methods". To find out (or prove) it, we can just write a simple test.

Take Math.pow for example, examining the Java code for java.lang.Math.pow(double a, double b), we will see:

 public static double pow(double a, double b) {
    return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}

But the JVM is free to implement it with intrinsics or runtime calls, thus the returning result can be different from what we would expect from StrictMath.pow.

And the following code shows this calling Math.pow() against StrictMath.pow()

//Strict.java, testing StrictMath.pow against Math.pow
import java.util.Random;
public class Strict {
    static double testIt(double x, double y) {
        return Math.pow(x, y);
    }
    public static void main(String[] args) throws Exception{
        final double[] vs = new double[100];
        final double[] xs = new double[100];
        final double[] ys = new double[100];
        final Random random = new Random();

        // compute StrictMath.pow results;
        for (int i = 0; i<100; i++) {
            xs[i] = random.nextDouble();
            ys[i] = random.nextDouble();
            vs[i] = StrictMath.pow(xs[i], ys[i]);
        }
        boolean printed_compiled = false;
        boolean ever_diff = false;
        long len = 1000000;
        long start;
        long elapsed;
        while (true) {
            start = System.currentTimeMillis();
            double blackhole = 0;
            for (int i = 0; i < len; i++) {
                int idx = i % 100;
                double res = testIt(xs[idx], ys[idx]);
                if (i >= 0 && i<100) {
                    //presumably interpreted
                    if (vs[idx] != res && (!Double.isNaN(res) || !Double.isNaN(vs[idx]))) {
                        System.out.println(idx + ":\tInterpreted:" + xs[idx] + "^" + ys[idx] + "=" + res);
                        System.out.println(idx + ":\tStrict pow : " + xs[idx] + "^" + ys[idx] + "=" + vs[idx] + "\n");
                    }
                }
                if (i >= 250000 && i<250100 && !printed_compiled) {
                    //presumably compiled at this time
                    if (vs[idx] != res && (!Double.isNaN(res) || !Double.isNaN(vs[idx]))) {
                        System.out.println(idx + ":\tcompiled   :" + xs[idx] + "^" + ys[idx] + "=" + res);
                        System.out.println(idx + ":\tStrict pow :" + xs[idx] + "^" + ys[idx] + "=" + vs[idx] + "\n");
                        ever_diff = true;
                    }
                }
            }
            elapsed = System.currentTimeMillis() - start;
            System.out.println(elapsed + " ms ");
            if (!printed_compiled && ever_diff) {
                printed_compiled = true;
                return;
            }

        }
    }
}

I ran this test with OpenJDK 8u5-b31 and got the result below:

10: Interpreted:0.1845936372497491^0.01608930867480518=0.9731817015518033
10: Strict pow : 0.1845936372497491^0.01608930867480518=0.9731817015518032

41: Interpreted:0.7281259501809544^0.9414406865385655=0.7417808233050295
41: Strict pow : 0.7281259501809544^0.9414406865385655=0.7417808233050294

49: Interpreted:0.0727813262968815^0.09866028976654662=0.7721942440239148
49: Strict pow : 0.0727813262968815^0.09866028976654662=0.7721942440239149

70: Interpreted:0.6574309575966407^0.759887845481148=0.7270872740201638
70: Strict pow : 0.6574309575966407^0.759887845481148=0.7270872740201637

82: Interpreted:0.08662340816125613^0.4216580281197062=0.3564883826345057
82: Strict pow : 0.08662340816125613^0.4216580281197062=0.3564883826345058

92: Interpreted:0.20224488115245098^0.7158182878844233=0.31851834311978916
92: Strict pow : 0.20224488115245098^0.7158182878844233=0.3185183431197892

10: compiled   :0.1845936372497491^0.01608930867480518=0.9731817015518033
10: Strict pow :0.1845936372497491^0.01608930867480518=0.9731817015518032

41: compiled   :0.7281259501809544^0.9414406865385655=0.7417808233050295
41: Strict pow :0.7281259501809544^0.9414406865385655=0.7417808233050294

49: compiled   :0.0727813262968815^0.09866028976654662=0.7721942440239148
49: Strict pow :0.0727813262968815^0.09866028976654662=0.7721942440239149

70: compiled   :0.6574309575966407^0.759887845481148=0.7270872740201638
70: Strict pow :0.6574309575966407^0.759887845481148=0.7270872740201637

82: compiled   :0.08662340816125613^0.4216580281197062=0.3564883826345057
82: Strict pow :0.08662340816125613^0.4216580281197062=0.3564883826345058

92: compiled   :0.20224488115245098^0.7158182878844233=0.31851834311978916
92: Strict pow :0.20224488115245098^0.7158182878844233=0.3185183431197892

290 ms 

Please note that Random is used to generate the x and y values, so your mileage will vary from run to run. But good news is that at least the results of compiled version of Math.pow match those of interpreted version of Math.pow. (Off topic: even this consistency was only enforced in 2012 with a series of bug fixes from OpenJDK side.)

The reason?

Well, it's because OpenJDK uses intrinsics and runtime functions to implement Math.pow (and other math functions), instead of just executing the Java code. The main purpose is to take advantage of x87 instructions so that performance for the computation can be boosted. As a result, StrictMath.pow is never called from Math.pow at runtime (for the OpenJDK version that we just used, to be precise).

And this arragement is totally legitimate according to the Javadoc of Math class (also quoted by @coobird above):

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

By default many of the Math methods simply call the equivalent method in StrictMath for their implementation. Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.

And the conclusion? Well, for languages with dynamic code generation such as Java, please make sure what you see from the 'static' code matches what is executed at runtime. Your eyes can sometimes really mislead you.

Top answer
1 of 4
2

You don't specifically need to import java.lang.Math, or any part of java.lang, and your snippet maybe contains some copy / paste errors, as explained in the comment by khelwood.

If we want to think about a more generic question on the topic if you are importing the whole class you are using more memory and if you are not using all the imported methods it doesn't make sense to import it all, it would be just a waste of resources.

Using an advanced IDE like IntelliJ you can enable automatic import and code analysis and this kind of best practises will be automatically suggested and enforced in your code with warnings and errors provided directly from the IDE. My suggestion is adopting a similar solution because it will speed up and improve your coding right away.

Or, if you don't like the idea of automatic import, you can use the Optimize Import function and obtain a similar result with a simple shortcut (control + alt + o).

2 of 4
1

The general way to introduce classes: import java.lang.Math.*;

The way to introduce classes statically: import static java.lang.Math.*;

The difference is that:

Generally, the introduction requires the use of ClassName.method(); to call the static method in the class;

public class Test {
     public static void main(String[] args) {
         System.out.println(Math.sqrt(4)); //Need to add the class name prefix
     }
}

After static introduction, use method(); directly to use static method.

import static java.lang.Math.*;
public class Test {
     public static void main(String[] args) {
         System.out.println(sqrt(4)); //Call the method directly
     }
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-class
Java Math Class - GeeksforGeeks
July 23, 2025 - Java.lang.Math Class methods help to perform numeric operations like square, square root, cube, cube root, exponential and trigonometric operations.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 19 › docs › api › java.base › java › lang › Math.html
Math (Java SE 19 & JDK 19)
December 12, 2022 - new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator. ... As the largest double value less than 1.0 is Math.nextDown(1.0), a value x in the closed range [x1,x2] where x1<=x2 may be defined by the statements
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.math
Math Class (Java.Lang) | Microsoft Learn
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. [Android.Runtime.Register("java/lang/Math", DoNotGenerateAcw=true)] public sealed ...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Math.html
Math (Java SE 21 & JDK 21)
January 20, 2026 - new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator. ... As the largest double value less than 1.0 is Math.nextDown(1.0), a value x in the closed range [x1,x2] where x1<=x2 may be defined by the statements
🌐
Tutorialspoint
tutorialspoint.com › home › java › java math class
Java Math Class Overview
September 1, 2008 - The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
🌐
Java Almanac
javaalmanac.io › jdk › 1.2 › api › java › lang › Math.html
Java Platform 1.2 API Specification: Class Math
Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. The remainder value is mathematically equal to f1 - f2 × n, where n is the mathematical integer closest to the exact mathematical value of the quotient f1/f2, and if two mathematical integers are equally close to f1/f2, then n is the integer that is even.
🌐
Android Developers
developer.android.com › api reference › math
Math | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
GitHub
github.com › openjdk-mirror › jdk7u-jdk › blob › master › src › share › classes › java › lang › Math.java
jdk7u-jdk/src/share/classes/java/lang/Math.java at master · openjdk-mirror/jdk7u-jdk
package java.lang; import java.util.Random; · · /** * The class {@code Math} contains methods for performing basic · * numeric operations such as the elementary exponential, logarithm, * square root, and trigonometric functions. * * <p>Unlike some of the numeric methods of class ·
Author   openjdk-mirror
🌐
JVM Gaming
jvm-gaming.org › performance tuning
Amazing java.lang.math performance - Performance Tuning - JVM Gaming
September 17, 2003 - Hi all, In researching for our Vector/Matrix classes and our chapter on fast java math, I have written some java.lang.math alternatives, typical techniques from the C/C++ world (look up tables, approximations, even ported a Quake3 inverse sqaure root routine ;-)) However, when attempting to benchmark, the JIT/VM is somehow doing AMAZING things with java.lang.math!
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › lang › Math.java
jdk/src/java.base/share/classes/java/lang/Math.java at master · openjdk/jdk
package java.lang; · import java.math.BigDecimal; import java.util.Random; import jdk.internal.math.FloatConsts; import jdk.internal.math.DoubleConsts; import jdk.internal.vm.annotation.IntrinsicCandidate; · import static java.lang.Double.*; · /** * The class {@code Math} contains methods for performing basic ·
Author   openjdk
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-math-class-java-set-2
Java.lang.Math Class in Java | Set 2 - GeeksforGeeks
July 23, 2025 - Syntax: public static double pow(double b,double e) Parameters: b : base e : exponent Returns: value as baseexponent JAVA code explaining incrementExact(), log10(), pow() method in lang.Math class.
🌐
Reddit
reddit.com › r/learnjava › how come i didn't need to import java.lang.math for my code?
r/learnjava on Reddit: How come I didn't need to import java.lang.Math for my code?
January 26, 2015 -

I had this line of code:

int generator = 1 + (int) (Math.random() * 19);

http://puu.sh/sGVps/b318650d6b.png

The program I create works perfectly but i was wondering why I didn't need to import lang.math despite using random.

🌐
Runestone Academy
runestone.academy › ns › books › published › javajavajava › ch5-math.html
From the Java Library: java.lang.Math
\( \newcommand{\lt}{<} ...$\scriptscriptstyle\phantom{\,#1\,}$}}} \) The java.lang.Math class provides many common mathematical functions that will prove useful in performing numerical computations....
🌐
FavTutor
favtutor.com › blogs › java-math-class
Math Class in Java & Methods (with Examples)
October 9, 2023 - In Java, you can use the Math class without importing it explicitly because it belongs to java.lang package. This allows you to access the methods and constants of the Math class directly using the class name.