Math Class Methods

 

Example to calculate a square root of 900.0

Math.sqrt(900.0)

Note all Math Methods are static! Static classes are NOT instantiated. So "Math" methods are available for use with explicit creating an object. Math is part of java.lang package, which is automatically imported by the compiler.

Math Methods

abs(x) -absolute value

ceil(x) - rounds x to the smallest integer not less than x

cos(x) - cosine

exp(x) - exponential method ex

floor(x) - rounds x to the largest integer not greater than x

log(x) - natural logarithm of x (base e)

max(x, y) - larger of x and y

min(x, y) - smaller of x and y

pow(x, y) - x raised to the power of y

sin(x) - sine of x

sqrt(x) - square root

tan(x) - tangent of x

The Math class also includes constants

Math.PI (3.1415926535979323846)

Math.E (2.7182818284590452354)

Random Numbers

To create a random number between 0 and 1 (not including 1)

double randomReal;

randomReal = Math.random();

To create an integer between 1 and 6 suitable for a result of a dice throw.

int die;

die = (int) (Math.random()*6) + 1

Note: You should also look at the Random class and tips from the Sun web site. http://developer.java.sun.com/developer/JDCTechTips/2001/tt0925.html

Primitive Data Types

Keyword

Description

Size/Format

(integers)

byte

Byte-length integer

8-bit two's complement

short

Short integer

16-bit two's complement

int

Integer

32-bit two's complement

long

Long integer

64-bit two's complement

(real numbers)

float

Single-precision floating point

32-bit IEEE 754

double

Double-precision floating point

64-bit IEEE 754

(other types)

char

A single character

16-bit Unicode character

boolean

A boolean value (true or false)

true or false

In method calls primitive data types are always passed by value! Reference types (the Wrapper Classes below) are always passed by reference.

Wrapper Classes (or Reference Types)

For each of the primitive data types in Java provides a corresponding "wrapper" class. At times you need to treat simple data types as objects. This is particularly true when we look at Collections. The use of primitive data types is the main area where Java departs from being a "pure" Object Oriented language.

These wrapper classes also feature a number of utility methods. Generally, these methods handle various tasks that you would otherwise write yourself. Become familiar with these methods.

Integer vs. int

Integer anInteger = new Integer(5); -> Create an Integer from an int

int anint = anInteger.intValue(); -> Obtain the int value of an Integer

int anint = Integer.parseInt("5"); -> Obtain the int value of a String

String aString = anInteger.toString() -> String representation of a Integer.

The Integer Class provides constants:

public static final int MIN_VALUE

The smallest value of type int. The constant value of this field is -2147483648.

public static final int MAX_VALUE

The largest value of type int. The constant value of this field is 2147483647