Some Built In Math Methods

 

Introduction.  We have already made extensive use of the methods showInputDialog( ) and showMessageDialog( ) as part of the JOptionPane class.  There are a very large variety of built-in methods in Java.  Unfortunately, it can be very difficult to find an appropriate one when you are looking for it. 

Since java.lang is the standard package used to execute java code we will focus on methods that are built into it for this webpage.Hopefully, this little survey will help in this.

The Math Class.  The following table contains a list of some of the more commonly useful built-in methods to be used for mathematical computations in the class Math. 

 

class Math  in (java.lang package)
Method

Description

Example
Math.abs(x) Returns the absolute value of x. 
Returns a type that matches the type of x.
Math.abs(-24.5) = 24.5
Math.abs(-2) = 2
Math.ceil(x) Returns the smallest integer which is not less than x. 
Returns a type double
Math.ceil(56.01) = 57.0
Math.exp(x) Returns ex where e~2.7182818284590455
Returns a type double
Math.exp(3) = 20.0855369
Math.floor(x) Returns the largest integer which is less than x. 
Returns a type double
Math.floor(56.01) = 56.0
Math.log(x) Returns the natural logarithm of x
Returns a type double
Math.log(2) = .6931471805
Math.max(x,y) Returns the maximum of x and y
Returns a type that matches the type of x and y
Math.max(15, 25) = 25
Math.max(45, 25.56) = 45.0
Math.min(x,y) Returns the minimum of x and y
Returns a type that matches the type of x and y
Math.min(15, 25) = 15
Math.min(45, 25.56) = 25.56
Math.pow(x,y) Returns xy where x and y are doubles
Returns a type double
Math.pow(2.0, 3.0) = 8.0
Math.pow(4.0, 0.5) = 2.0
Math.round(x) Returns the integer which is closest to x. 
Returns a type double
Math.round(25.51) = 26.0
Math.round(25.4999) = 25.0
Math.sqrt(x) Returns the square root of x where x is a double. 
Returns a type double
Math.round(6.25) = 2.5
Math.round(16.0) = 4.0

 

I am assuming that most of these functions are at least somewhat familiar to you so I won't work any examples at present.  Though we are likely to make use of some of these in the future.

The next couple webpages will cover some built in methods in classes for strings and characters.