2.9 Using the Math Class

Enduring Understanding

Some objects or concepts are so frequently represented that programmers can draw upon existing code that has already been tested, enabling them to write solutions more quickly and with a greater degree of confidence.

Learning Objective

Call static methods.

Essential Knowledge

Static methods are called using the dot operator along with the class name unless they are defined in the enclosing class.

Enduring Understanding

The way variables and operators are sequenced and combined in an expression determines the computed result.

Learning Objective

Evaluate expressions that use the Math class methods.

Essential Knowledge

The Math class is part of the java.lang package.

The Math class contains only static methods.

The following static Math methods—including what they do and when they are used—are part of the Java Quick Reference:

  • int abs(int x) — Returns the absolute value of an int value

  • double abs(double x) — Returns the absolute value of a double value

  • double pow(double base, double exponent) — Returns the value of the first parameter raised to the power of the second parameter

  • double sqrt(double x) — Returns the positive square root of a double value

  • double random()— Returns a double value greater than or equal to 0.0 and less than 1.0

The values returned from Math.random can be manipulated to produce a random int or double in a defined range.

Last updated

Was this helpful?