Page 241 - Computer science 868 Class 12
P. 241
8.8 BUILT-IN METHODS
There are different built-in methods in Java. They are categorised as:
1. Mathematical methods
2. Character functions
3. Methods of wrapper class
4. Methods of String class
8.8.1 Mathematical Methods
To use mathematical methods in programs in Java, we must use the “Math” class which resides in the “java.lang”
package.
Syntax to use mathematical methods is:
Math.function name(arguments)
The different mathematical methods are as follows:
1. Math.min(): This method finds the minimum number between two numbers that are passed as arguments.
double d = Math.min(1.2,5.3) // Output: 1.2
The method returns the type of value passed to it, i.e., if double value is passed, then the return type will be
double. Similarly if int value is passed, then the return type is int.
2. Math.max(): This method finds the maximum number between two numbers that are passed as arguments.
double d = Math.max(1.2,5.3) // Output 5.3
The method returns the type of value passed to it, i.e., if double value is passed, then the return type will be
double. Similarly, if int value is passed, then the return type is int.
3. Math.sqrt(): This method is used to return the square root of the argument passed. It returns the value in double.
double d = Math.sqrt(9) // Output: 3.0
4. Math.cbrt(): This method returns the cube root of the number passed in the argument. The return type is double.
It can work both on both positive and negative values.
double d = Math.cbrt(27); // Output: 3.0
double d = Math.cbrt(-64); // Output: -4.0
5. Math.pow(): This method is used to return the “nth” power of a number “m” in the format m . It returns the value
n
in double. It can work both on positive and negative values.
double d =Math.pow(3,2) // Output: 9.0
double d = Math.pow(-3,-2); // Output: 0.1111111111111111
[Explanation: -1.0/-9.0]
6. Math.log(): This method returns the logarithm of a value provided in the argument. It always returns a double-
type value. It works on positive numbers only.
double d = Math.log(5); // Output: 1.6094379124341003
double d = Math.log(-4.28); // Output: NaN
7. Math.abs(): This method is used to return the positive value of any number. It works both on integers as well as
real values.
double d = Math.abs(-2.7); // Output: 2.7
int i = Math.abs(4); // Output: 4
239
Methods 239

