In Java, various mathematical functions such as root, absolute value, sine and other trigonometric functions are often used.
To use them, we need to get to know the Math library from the java.lang package. A few of the most important functions are below.
 sqrt (double number) – returns the root of the number double. As a parameter we can specify any numeric type, then it will be automatically converted it to double.
 pow (double a, double b) – returns the number a raised to the power of b
 abs (number) – the parameter can be any number, the method returns the absolute value from the argument
 you can learn more about these methods here in Math Class
All methods are static, to invoke them use the structure:
1

Math.method_name(argument)

Let’s see it on a simple example of a program that calculates the root of a number and then raises it to the 2rd power.
1
2
3
4
5
6
7
8
9
10
11

public class MathTest{
public static void main(String[] args){
double number= 7.0;
int b = 2;
double root = Math.sqrt(number);
double power = Math.pow(number, b);
System.out.println("Root from "+number+" equals: "+root);
System.out.println("Number "+number+" raised to "+b+" to "+power);
}
}

First, we declare variables: number – our main number, b – power, and then using mathematical functions we calculate the root and power. At the end, we display our results on the console.
An interesting thing worth mentioning here is the static import. Thanks to its application, we will be able to omit the prefixes of Math in front of function names. Below is the example.
1
2
3
4
5
6
7
8
9
10
11

public class MathTest{
public static void main(String[] args){
double number= 7.0;
int b = 2;
double root = sqrt(number); //notice here
double power = pow(number, b); //notice here
System.out.println("Root from "+number+" equals: "+root);
System.out.println("Number "+number+" raised to "+b+" to "+power);
}
}

As you can see before the start of the class body, we had to add a header in the form:
1

import static java.lang.Math.*;

This improves the readability of our code.
In the Math class, there are also two constant PIs and Es, thanks to them we do not have to declare our own pi and e numbers. We call them like functions.
1
2

Math.PI
Math.E

Big Numbers
It may happen that even the range of long or double types is not enough for our calculations. What to do then? In Java, there are two classes to store really huge numbers that offer additional mathematical functions.
 BigInteger – a class for large integers
 BigDecimal – a class for large floating point numbers
Their use in traditional programs is not very convenient, because you can not directly assign the BigInteger value to the int value, even though BigInteger would store a number in the int range. This is due to the fact that the BigInteger class is an object type. Both classes also have constants, which are often used, the most important are:
 BigInteger.ONE / BigDecimal.ONE
 BigInteger.ZERO / BigDecimal.ZERO
To create a new BigInteger or BigDecimal type variable, we must first create an object. Don’t forget to import your class.
1
2
3
4
5
6
7
8

import java.math.BigInteger;
public class BigNumber{
public static void main(String[] args){
BigInteger bigNum= new BigInteger("987654323456789876543456");
System.out.println(bigNum.toString());
}
}

First, we create a large number of type, notice that we give it in the form of a String. However, you can also use a different form.
1

BigInteger wielkaLiczba = BigInteger.valueOf(98765432);

In this case, however, remember that the valueOf () method argument should not exceed the long or double range for the BigDecimal class. With the String method they can be practically unlimited. To add two huge numbers, we can not use standard operators, use: add (), subtract (), multiply (), divide ().
1
2
3
4
5
6
7
8
9
10

import java.math.BigInteger;
public class BigNumbers{
public static void main(String[] args){
BigInteger a = new BigInteger("213456568798786576453242352352");
BigInteger b = new BigInteger("987623412415353141321312425152");
BigInteger total= a.add(b);
System.out.println("Total "+total.toString());
}
}

Here we created two big numbers a and b. Then we calculated their sum thanks to the construction of a.add (b), which returns the result of adding two BigInteger numbers. We display the result thanks to the fact that the BigInteger class has a toString () method that converts the number to a string.