![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions | |
Math functions are used for performing various Mathematical
operations. These functions are located in the Math class of java.lang package (java.lang.Math
)
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Math class contains the 2 main details called as field and method
Field Detail
Fields such as E = 2.718281845
and PI = 3.14159265358
constants are stored.
Method Detail
All other mathematical functions are stored
Method | Syntax | Example | Result |
---|---|---|---|
abs() Returning the absolute value of a double value. |
static double abs(double) | Math.abs(-9876) | 9876 |
ceil() This method returns the smallest (closest to negative infinity) double value. |
static double ceil(double) | Math.ceil(414.9) | 415 |
floor() This method returns the largest (closest to positive infinity) double value. |
static double floor(double) | Math.floor(621.2) | 621 |
exp() This method returns e raised to the power of a double value. |
static double exp(double) | Math.exp(5) | 148.41315910 |
min() This method returns the smaller of two values. |
static int min(int,int) | Math.min(12,29) | 12 |
max() This method returns the largest of two values. |
static int max(int,int) | Math.max(82,59) | 82 |
pow() This method returns the value of the first argument raised to the power of the second argument. |
static double pow(double,double) | Math.pow(2,3) | 8 |
round() This method returns the closest int to the argument. |
static int round(float) | Math.round(1654.9874f) | 1655 |
sqrt() This method returns the correctly rounded positive square root of a double value. |
static double sqrt(double) | Math.sqrt(25) | 5.0 |
In above functions abs(),min(),max() are overloaded in java. that means you can pass int,float,double and long values also, Based on it they return same type value.
There are more math functions available. but above are the mostly used while developing an application.
class MathFunctions { public static void main(String args[]) { int first, second; first = Integer.parseInt(arg[0]); second = Integer.parseInt(arg[1]); System.out.println("Addition : "+ (first + second)); System.out.println("Subtraction : "+ (first - second)); System.out.println("Multiplication : "+ (first * second)); System.out.println("Division : "+ (first / second)); } }