Importing Math in Java
The Math class lives in java.lang, which means you don't need an import statement at all. It's auto-imported by the compiler just like String or Integer. You can call Math.pow(), Math.sqrt(), or Math.random() directly from any class without declaring anything. If you still want to import it explicitly, you can write: But again, this does nothing different than not writing it. The class is already available.
Sometimes I see people write static imports instead, and that's where things get interesting:
import static java.lang.Math.*;
import static java.lang.Math.PI;
import static java.lang.Math.pow;
A static import lets you call methods without the Math. prefix. So instead of Math.sqrt(x) you write sqrt(x). This is the version of "importing Math" that actually changes how you write code. I use this when I'm doing heavy math in a single method. Otherwise the unqualified calls get noisy fast. One edge case I ran into: if you're writing a custom method or variable named sqrt in your own class, the static import will shadow it. The compiler won't error out, it'll just use the imported Math.sqrt instead of yours. I spent about twenty minutes debugging a geometry utility where my own sqrt helper was silently ignored because I had import static java.lang.Math.* at the top of the file. The fix was renaming my method. I don't recommend mixing static math imports with custom methods that share names. There's also a practical limitation worth noting. The Math class covers the basics but it's not a full math library. No matrix operations, no complex numbers, no symbolic algebra. If your project needs anything beyond trigonometry, logarithms, and basic constants, you'll eventually hit the wall. Most teams switch to Apache Commons Math or Jama for that. The Math class itself is fine for simple tasks but it wasn't built for serious numerical work.
Get the Full Details

Common pitfall: people assume Math.random() gives uniform distribution across floating point ranges the same way other libraries do. It returns a double between 0.0 (inclusive) and 1.0 (exclusive), which is standard, but if you're generating numbers for simulations or cryptography you should be using java.security.SecureRandom or a proper statistical library instead. Math.random() isn't cryptographically secure and its underlying implementation hasn't changed in years. Another thing nobody warns about: floating point precision. Math operations use IEEE 754 doubles by default. 0.1 + 0.2 does not equal 0.3 exactly. This isn't an import problem, but it's the first thing that trips people up after they've already figured out how to call Math methods. If you need exact decimal arithmetic, use BigDecimal instead and skip the Math class for that part of the code. To summarize the actual steps:
- For regular access, just write Math.methodName() with no import needed.
- For cleaner syntax, add a static import at the top of your file.
- Watch out for name collisions if you also define your own methods.
- Don't use Math for anything that requires precision beyond double, and don't rely on it for anything security-related.