Home >>Java Tutorial >Java Math(Numbers)
The primitive data types that are like: byte, int, long, double, etc are generally used whenever working with the Java Numbers. However, there are some situations that require the programmer to use the objects in the place of primitive data types in the development. Java delivers us the wrapper classes that are basically for achieving this. Double, Float, Integer, Short, Long, Byte these all are wrapper classes and can be called as the subclasses of the abstract class Number.
Here is an example
int i = 5000; float gpa = 13.65; double mask = 0xaf;
Java Math classes are known to deliver various methods that are used to work on math calculations like max(), avg(), sin(), min(),abs(), cos(), tan(),ceil(), floor(), round() etc.
Not like some of the StrictMath class numeric methods, all the implementations of the equivalent function of Math class in Java can't defined to return the bit-for-bit same results. This relaxation that is provided by the Java permits the implementation with better-performance where the strict reproducibility is not generally required.
An Arithmetic Exception is thrown by the methods multiplyExact(), addExact(), subtractExact(), and toIntExact()when there is a case where the size is int or long and the results overflow the range of the value in the Java language.
The overflow of the value for other arithmetic operations like absolute value, increment, decrement, divide, and negation generally occurs only with a specific minimum or maximum value. This should be mandatorily checked against the maximum and minimum value as appropriate.
Here are some of the example of the Java Numbers that will clear your concept about the topic:
public class JavaMathExample1 { public static void main(String[] args) { double a = 20; double b = 4; //Print Maximum Value System.out.println("Max value of a and b = " +Math.max(a, b)); //Print square root of b System.out.println("b square root = " + Math.sqrt(b)); //print power of 4 System.out.println("Here is the Power of 4 = " + Math.pow(b)); //print the logarithm of a and b System.out.println("Here is Logarithm of a = " + Math.log(a)); System.out.println("Here is Logarithm of b= " + Math.log(b)); } }