# Java Math Class: java.lang.Math

Use the Math class, part of java.lang.Math. Call Math.floor, ceil and other methods.**Math.E, PI.** Sometimes programs need to use the E and pi constants. With the Math class, we can access these as fields. We do not need to specify 3.14 for pi.

**Tip:** Using these included constants results in much clearer code—and no mistakes in the value of pi.

**Java program that gets Math.E, PI**
public class Program {
public static void main(String[] args) {*
// Get these known constants.
*double value1 = __Math.E__;
double value2 = __Math.PI__;
System.out.println(value1);
System.out.println(value2);
}
}
**Output**
2.718281828459045
3.141592653589793

**Exact methods.** The Math class provides "exact" methods. These do what we expect (add, subtract, multiply) but throw an ArithmeticException when the result overflows.

**So:** In a computation where overflow would be disastrous, these methods help check that the program is correct.

**Part 1:** Here we call the "exact" methods with 1 or 2 arguments. We store the results of the methods in locals.

**Part 2:** We display the results of all the method invocations. For the "addExact" method, 1 + 1 equals 2.

**Part 3:** We call multiplyExact with 2 arguments that will cause an overflow. An exception is thrown, and printed to the console.

**Java program that uses addExact, exact methods**
import java.lang.Math;
public class Program {
public static void main(String[] args) {*
// Part 1: use exact methods.
*int result1 = __Math.addExact__(1, 1);
int result2 = Math.addExact(100, 1);
int result3 = Math.subtractExact(100, 1);
int result4 = Math.multiplyExact(5, 4);
int result5 = Math.incrementExact(2);
int result6 = Math.decrementExact(1000);
int result7 = Math.negateExact(100);*
// Part 2: display our results.
*System.out.println(result1 + *" "* + result2 + *" "* + result3 + *" "*
+ result4 + *" "* + result5 + *" "* + result6 + *" "* + result7);*
// Part 3: an ArithmeticException is thrown if a number overflows.
*try {
int invalid = Math.multiplyExact(Integer.MAX_VALUE, 100);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
**Output**
2 101 99 20 3 999 -100
java.lang.ArithmeticException: integer overflow

**Math.toIntExact.** The Math class has some conversion methods. Here, we use toIntExact, which converts along value to an int. The int must be able to store the long's value.

**Example:** The value 100 can be stored in an int, so the first toIntExact call succeeds. But Long.MAX_VALUE cannot be stored in an int.

**Java program that uses Math.toIntExact**
import java.lang.Math;
public class Program {
public static void main(String[] args) {*
// An int can store the value 100.
*long test = 100;
int result = Math.__toIntExact__(test);
System.out.println(*"INT: "* + result);*
// This will cause an exception, as the int cannot store the value.
*test = Long.MAX_VALUE;
result = Math.__toIntExact__(test);
}
}
**Output**
INT: 100
Exception in thread "main" java.lang.ArithmeticException: integer overflow
at java.base/java.lang.Math.toIntExact(Math.java:1071)
at Program.main(Program.java:13)

**With abstraction,** we conceal incredible levels of complexity. This is called information hiding. With the Math class, we use declarative method calls to compute values.

**As developers,** we can avoid implementing this code ourselves. Instead we use prebuilt, tested, optimized methods. In most cases (not all) this approach is best.