Math Class

The class Math contains methods for performing basic numeric operations such as elementary exponential, logarithm, square root, and trigonometric functions.


Exponent Methods

Rounding Methods

Service Methods

The methods abs(), max(), and min() are overloaded. type could be any type - short, int, long, float, or double.

Trigonometric Methods


A recursive method is a method that calls itself. An iterative method is a method that uses a loop to repeat an action. Anything that can be done iteratively can be done recursively, and vice versa. Iterative algorithms and methods are generally more efficient than recursive algorithms.

Recursion is based on two key problem solving concepts: divide and conquer and self-similarity. A recursive solution solves a problem by solving a smaller instance of the same problem. It solves this new problem by solving an even smaller instance of the same problem. Eventually, the new problem will be so small that its solution will either be obvious or known. This solution will lead to the solution of the original problem.

A recursive definition consists of two parts: a recursive part in which the nth value is defined in terms of the (n-1)th value, and a non recursive boundary case or base case which defines a limiting condition. An infinite repetition will result if a recursive definition is not properly bounded. In a recursive algorithm, each recursive call must make progress toward the bound, or base case. A recursion parameter is a parameter whose value is used to control the progress of the recursion towards its bound.

Procedure call and return in Java uses a last-in-first-out protocol. As each method call is made, a representation of the method call is place on the method call stack. When a method returns, its block is removed from the top of the stack.

Use an iterative algorithm instead of a recursive algorithm whenever efficiency and memory usage are important design factors. When all other factors are equal, choose the algorithm (recursive or iterative) that is easiest to understand, develop, and maintain.

Here is an example of a recursive method that calculates the factorial of n. The base case occurs when n is equal to 0. We know that 0! is equal to 1. Otherwise we use the relationship n! = n * ( n - 1 )!

public static long fact ( int n )
  if ( n == 0 )
    return 1;
    return n * fact ( n - 1 );