The Math class is built into Java, so you import it with a single line at the top of your file
Java's Math class lives in the java.lang package, which is automatically available to every Java program. You do not need to download anything or add external libraries. To use Math functions like square root, absolute value, or rounding, add this line at the very top of your file, before your class declaration:
import java.lang.Math;
In practice, most programmers skip this import entirely. Since java.lang is imported by default, you can call Math functions directly by writing Math.sqrt(), Math.abs(), or Math.round() without the import statement. The import line is optional but makes your code slightly cleaner if you use Math functions many times in one file.
Once imported (or without importing), you call Math methods by typing Math. followed by the function name and your input in parentheses. For example, Math.sqrt(16) returns 4.0, and Math.abs(-5) returns 5.
Key Takeaways
- Java's Math class is part of java.lang, which is automatically available, so you can use Math functions without writing an import statement.
- If you want an explicit import, write import java.lang.Math; at the top of your file before the class declaration.
- Call Math functions by typing Math. followed by the method name, such as Math.sqrt(25) or Math.max(10, 20).
- Common Math methods include sqrt() for square root, abs() for absolute value, round() for rounding, pow() for exponents, and random() for generating random numbers.
Common Math methods and what they do
The Math class includes dozens of methods. Here are the ones you will use most often:
| Method | What it does | Example |
|---|---|---|
| Math.sqrt(x) | Square root of x | Math.sqrt(9) returns 3.0 |
| Math.abs(x) | Absolute value (removes the negative sign) | Math.abs(-7) returns 7 |
| Math.round(x) | Rounds to the nearest whole number | Math.round(4.6) returns 5 |
| Math.pow(x, y) | x raised to the power of y | Math.pow(2, 3) returns 8.0 |
| Math.max(x, y) | The larger of x or y | Math.max(5, 10) returns 10 |
| Math.min(x, y) | The smaller of x or y | Math.min(5, 10) returns 5 |
| Math.random() | Random decimal between 0.0 and 1.0 | Math.random() might return 0.427 |
| Math.floor(x) | Rounds down to the nearest whole number | Math.floor(4.9) returns 4.0 |
| Math.ceil(x) | Rounds up to the nearest whole number | Math.ceil(4.1) returns 5.0 |
Each method takes one or more numbers as input and returns a result. You can nest them together — for example, Math.round(Math.sqrt(25)) calculates the square root first, then rounds the result.
A real example: calculating the distance between two points
Here is how you might use Math methods together in an actual program. This code calculates the distance between two points on a grid using the distance formula:
double x1 = 0, y1 = 0, x2 = 3, y2 = 4; double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); System.out.println(distance);
This code uses Math.pow() to square the differences, adds them together, and then uses Math.sqrt() to find the square root. The result is 5.0, which is the distance between the two points. Notice that you do not need an import statement — the Math class is available automatically.
Math constants: PI and E
The Math class also provides two mathematical constants that you can use directly: Math.PI (approximately 3.14159) and Math.E (approximately 2.71828). You reference them the same way you call methods, but without parentheses.
For example, to calculate the area of a circle with radius 5, you would write:
double radius = 5; double area = Math.PI * Math.pow(radius, 2); System.out.println(area);
This returns approximately 78.54. Using Math.PI instead of typing 3.14159 yourself ensures precision and makes your code clearer to read.
Why you might use a static import instead
If your code uses Math functions very frequently, you can write a static import to avoid typing Math. every time:
import static java.lang.Math.*;
With this import, you can write sqrt(16) instead of Math.sqrt(16), and PI instead of Math.PI. This is less common in professional code because it can make it harder to see where a function comes from when you read the code later. Most programmers stick with the regular Math.method() style for clarity.
Rounding: round(), floor(), and ceil() are different
Three Math methods round numbers, but they work differently. Math.round() uses standard rounding rules: 4.4 becomes 4, and 4.5 becomes 5. Math.floor() always rounds down, so 4.9 becomes 4. Math.ceil() always rounds up, so 4.1 becomes 5.
Choose the one that matches what your program needs to do. If you are calculating money and need to round to the nearest cent, use Math.round(). If you are allocating resources and cannot go over a limit, use Math.floor(). If you need to ensure you have enough of something, use Math.ceil().
Frequently Asked Questions
Do I have to import Math to use it?
No. The java.lang package is imported automatically in every Java program, so you can call Math.sqrt() or any other Math method without writing an import statement. Writing import java.lang.Math; is optional and does not change how your code works.
What is the difference between Math.round() and Math.floor()?
Math.round() uses standard rounding: 4.4 becomes 4, and 4.5 becomes 5. Math.floor() always rounds down, so 4.9 becomes 4.0. Use round() for typical rounding and floor() when you need to round down no matter what.
Can I use Math methods with integers and decimals?
Yes. Most Math methods work with both integers and decimals (called int and double in Java). However, methods like sqrt() and pow() always return a decimal, even if the result is a whole number. If you need an integer result, you can cast it: (int) Math.sqrt(16) returns 4 instead of 4.0.
What does Math.random() return?
Math.random() returns a random decimal number between 0.0 (inclusive) and 1.0 (exclusive). To get a random integer between 1 and 10, you would write (int)(Math.random() * 10) + 1. Each time you call it, you get a different number.