SwadhyAI Notes

All Java notes

Java Math Methods Reference

›Short versionOpen if you only have a minute.
  • Math is a utility class. Every method here is static, and none of them keep an object you construct.
  • sin, cos, and tan take radians. Pass the result of toRadians when the angle you have is in degrees.
  • sqrt of a negative number returns NaN. It does not throw.
  • addExact throws ArithmeticException on overflow. The ordinary + operator wraps instead.
  • Math.abs(Integer.MIN_VALUE) is still negative. The positive value does not fit in an int.
  • Math.random() returns a double from 0.0 inclusive up to, but not including, 1.0.

Math is a final utility class of static methods and constants. It covers common numeric operations, but it is not a decimal-money library and it does not keep random-generator state you can control.

Methods

APIParametersReturnsBehaviorWatch for
max(x, y) / min(x, y)Two numeric values of compatible primitive overloads.The larger or smaller value.Compares without sorting or modifying anything.Floating-point NaN propagates through comparisons.
sqrt(double x)Value whose square root is needed.double square root.Returns NaN for negative inputs instead of throwing.Use Math.hypot(x, y) for distance to avoid intermediate overflow.
pow(double a, double b)Base and exponent.double result.Computes a raised to b using floating-point arithmetic.For integer powers in exact business logic, consider loops or BigInteger.
round(float/double x)Floating-point value.int for float, long for double.Rounds to nearest whole number with halves toward positive infinity.Return type changes by overload.
toRadians(double deg) / toDegrees(double rad)Angle value.Converted angle as double.Converts units before or after trig calls.sin(90) is not 1 unless 90 is converted to radians.
addExact(int x, int y)Two int or long values.Exact sum.Throws if the operation overflows the primitive range.Normal + silently overflows for int and long.

Which call fits

OptionBest forTradeoff
Math.random()Quick demos and simple random double values.No direct seed control and awkward for ranges.
java.util.RandomRepeatable pseudo-random sequences and simple games.Older API; not ideal for concurrent high-performance use.
BigDecimalMoney and exact decimal rounding rules.More verbose than Math and primitive doubles.

Examples

Random integer in a range

java
int min = 10;
int max = 20;
int value = min + (int) (Math.random() * (max - min + 1));
System.out.println(value);

Safe integer arithmetic

java
try {
    int total = Math.addExact(2_000_000_000, 900_000_000);
    System.out.println(total);
} catch (ArithmeticException e) {
    System.out.println("Overflow detected");
}

What does Math.sin(90) return, and what call actually asks for the sine of a right angle?

Math.sin(90) treats 90 as radians, which is many turns past a right angle, so the result is about 0.89, not 1. A right angle is Math.sin(Math.toRadians(90)), which is 1.0. Math.sqrt(-1) is a different quiet failure: it returns NaN and does not throw.