The Math class is a toolbox of static methods for arithmetic. The wrapper classes Integer and Double let primitive values act like objects and carry useful constants and conversion methods.
Math methods you must know
| Call | Returns | Result |
|---|---|---|
| Math.abs(-7) | absolute value | 7 |
| Math.pow(2, 10) | power (always a double) | 1024.0 |
| Math.sqrt(144) | square root (a double) | 12.0 |
| Math.random() | a double in [0.0, 1.0) | e.g. 0.374... |
Note that pow and sqrt return double values, even for whole-number results.
Random integers from Math.random()
Math.random() returns a double that is at least 0.0 and strictly less than 1.0. To get a random int from min to max inclusive, use this formula — it is worth memorizing exactly.
// random int in [min, max] inclusive:
int r = (int)(Math.random() * (max - min + 1)) + min;
// a dice roll, 1 through 6:
int roll = (int)(Math.random() * 6) + 1;
Wrapper classes: Integer and Double
Integer wraps an int; Double wraps a double. Java autoboxes (wraps) and unboxes (unwraps) automatically. The wrappers also hold constants and converters like Integer.parseInt and Integer.MAX_VALUE.
Integer boxed = 5; // autoboxing: int -> Integer
int back = boxed; // unboxing: Integer -> int
int n = Integer.parseInt("42"); // text -> int
System.out.println(n + 1);
Output:
43
Worked example 1
Absolute value and powers.
System.out.println(Math.abs(-15));
System.out.println(Math.pow(3, 2));
System.out.println((int) Math.pow(3, 2));
Output:
15
9.0
9
abs of an int is an int (15). pow returns a double (9.0); casting gives the int 9.
Worked example 2
Read the random formula.
int x = (int)(Math.random() * 10) + 5;
What range of integers does this produce?
Math.random() * 10 is in [0, 10); casting to int gives 0..9; adding 5 shifts it to 5..14 inclusive.
Worked example 3
Build a specific range.
int n = (int)(Math.random() * 100) + 1;
Write an expression for a random integer from 1 to 100 inclusive.
The span 1..100 has 100 values, so multiply by 100 (giving 0..99) and add 1 → 1..100.
Worked example 4
Parse text into a number.
int a = Integer.parseInt("100");
double b = Double.parseDouble("2.5");
System.out.println(a * b);
Output:
250.0
parseInt turns “100” into 100; parseDouble turns “2.5” into 2.5. The product mixes int and double, so the result is a double: 250.0.
Worked example 5
Autoboxing and a wrapper constant.
Integer score = 95; // autoboxed
System.out.println(score + 5);
System.out.println(Integer.MAX_VALUE);
Output:
100
2147483647
score unboxes to 95 for the arithmetic → 100. Integer.MAX_VALUE is the largest int Java can store.
Common trap: Math.pow returns a double
Math.pow(2, 3) is 8.0, not 8. If you need an int, you must cast: (int) Math.pow(2, 3). The same goes for Math.sqrt. Forgetting this causes “won't compile” or “wrong type” answers.
Hack: The dice formula, decoded
Lock in (int)(Math.random() * (max - min + 1)) + min. Read it as: the span is max - min + 1 (how many values), the cast turns the [0, span) double into a whole number 0..span−1, and the + min slides the start to where you want it.
And tattoo this on your brain: Math.pow and Math.sqrt hand back doubles. Need an int? Cast.