Java Math Class Explained [Easy Examples]

 

Introduction to Java Math Class

Java contains a set of built-in math operators for performing simple and complex mathematical operations on Java variables. The Java built-in math operators are reasonably simple and perform simple operations. For complex operations, Java contains the Java Math class which has methods for performing more advanced math calculations.

In this tutorial, we will cover java mathematical operations, where first we will discuss the built-in simple operations and then will have a look at java math class and will use some of the java math class's methods to solve various problems. All in all, this tutorial is going to have all the details about mathematical calculations and operations that you can perform using the java math class and java mathematical operators.

Advertisement

 

Getting started with mathematical operators

Arithmetic or mathematical operators in java are used to perform different mathematical calculations. In java, there are five different types of arithmetic operators which perform different calculations. these operators are addition, multiplication, division, subtraction, and modulus. Below is a list of these operators:

  • + ; used for the addition of numbers
  • - ; used for subtraction of numbers
  • * ; used for multiplication of numbers
  • /; used for division of numbers
  • % ; used to find the modulus of a number

You can read and learned about these operators in more detail; But here let us take some simple examples and used these mathematical operators.

 

Example of Java mathematic operations

we already know the five different types of java mathematic operations, now let us take an example and see how they actually work in the java programming language. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 
       // creating java variables
       int num1= 20;
       int num2 = 3;

   // addition java math operator
   System.out.println("addition  : "+(num1 + num2));

   // subtraction java math operator
   System.out.println("subtraction : "+(num1 - num2));

   // java math multiplication operation
   System.out.println("Multiplication: "+(num1 * num2));

   // java math division operator
   System.out.println("division : "+(num1 / num2));

   // java math modulus operator
   System.out.println("Modulus : "+(num1 % num2));
   }
}

Output:

addition : 23
subtraction : 17
Multiplication: 60
division : 6
Modulus : 2

Notice that we get the desired output.

 

Java integer and floating division

In the previous example, we had seen that when we divide 20 by 3 we got, 6 not 6.66666 because both 3 and 20 were integers so we get an integer value rather than floating. In this section, we will learn how we can get a floating value after the division of two numbers. Let us take an example of integer division and then we will get a floating division.

Advertisement

See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 


   // java math division operator
   System.out.println("division : "+(25 / 2));
   }
} 

Output:

division : 12

Now let us see how we can get the exact value after division. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // java math division operator
   System.out.println("division : "+(25 / 2f));
   }
}<code>

Output:

division : 12.5

Notice that this time, we were able to get a floating value because while dividing if had noted we wrote 2f to tell the Java that this is floating and as a result, the Java returns a floating value.

 

Java Math Class

The Java Math class provides more advanced mathematical calculations than what the basic Java math operators provide. The java math class contains methods for finding the maximum or minimum of two values, rounding values, logarithmic functions, square root, and trigonometric functions (sin, cos, tan etc.). It is located in the java.lang package. Thus, the fully qualified class name of the Math class is java.lang.Math .

Here is a list of some of the methods that we will discuss in in section in java math class. See the list below:

  • Math.abs() : used to find the absolute value of a number.
  • Math.ceil()  : Return the ceil of the number.
  • Math.floor() : Returns the floor of the number.
  • Math.min()  : used to find the minimum value.
  • Math.max() : Used to find the maximum value.
  • Math.round() : Used to rounding decimal number.
  • Math.random()  : used to create a random number.
  • Math.pow() : Used to find the power of the number.
  • Math.sqrt() : returns the squre root of a number.
  • Math.log() : use to find the log.

Now let us learn each of these java math methods in more details by taking various examples.

Advertisement

 

Math.abs() method in java math

The Math.abs() function returns the absolute value of the parameter passed to it. It only takes one parameter at atime. The absolute value is the positive value of the parameter. If the parameter value is negative, the negative sign is removed and the positive value corresponding to the negative value without sign is returned.

Here is a simple syntax of java math.abs() method.

Math.abs(number);

Now let us take an example and use the absolute method and see how it works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // creating variables
   int num1 = 3;
   int num2 = -5;

   // applying java math absolute method
   System.out.println("absulote value is : "+Math.abs(num1));
   System.out.println("absulote value is : "+Math.abs(num2));
   }
} 

Output:

absulote value is : 3
absulote value is : 5

Notice that the absolute method returns positive value for negative one and positive for positive one.

 

Math.ceil() method in Java math

The Math.ceil() function rounds a floating point value up to the nearest integer value. It can return value greater than or equal to the input one. It cannot return value less than the input.

Here is a simple syntax of java ceil method.

Math.ceil(number);

Now, let us take some examples and see how we can use java ceil method in java programming language. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // creating variables
   float num1 = 3.1f;
   float num2 = -5.5f;

   // applying java math ceil method
   System.out.println("ceil value is : "+Math.ceil(num1));
   System.out.println("ceil value is : "+Math.ceil(num2));
   }
}

Output:

ceil value is : 4.0
ceil value is : -5.0

Notice the ceil method returns the nearest whole number value but greater than or equal to the input one.

 

Math.floor() method in Java math

The Math.floor() function rounds a floating point value down to the nearest integer value. The rounded value is returned as a double.It can return a value less than or equal to the input but cannot return a value greater then input.

The simple syntax looks like this.

Math.floor(number);

Now let us take an example and see how the java floor method works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // creating variables
   float num1 = 3.1f;
   float num2 = -5.5f;

   // applying java math floor method
   System.out.println("floor value is : "+Math.floor(num1));
   System.out.println("floor value is : "+Math.floor(num2));
   }
} 

Output:

floor value is : 3.0
floor value is : -6.0

Notice that for each case, we get value less then the input one.

 

Math.min() method in Java math

The Math.min() method returns the smallest of two values passed to it as parameter. It takes at onlt two parameters and return the smallest among them.

The simple syntax looks like this.

Math.min(num1, num2);

Now let us take example and see how it actually works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // creating variables
   int num1 = 3;
   int num2 = -5;

   // applying java math min method
   System.out.println("minimum value is : "+Math.min(num1, num2));
   }
}

Output:

minimum value is : -5

Notice that we had successfully returned the minimum value.

 

Math.max() method in java math

Similar to the java minimum method, The Math.max() method takes only two parameters and returns the largest them.

The simple syntax looks like this;

Math.max(num1, num2);

Now, let us take an example and see how it actually works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // creating variables
   int num1 = 3;
   int num2 = -5;

   // applying java math man method
   System.out.println("maximum value is : "+Math.max(num1, num2));
   }
}  

Output:

maximum value is : 3

Notice that it returns the largest value.

 

Math.round() method in Java math

The Math.round() method rounds a float or decimal number to the nearest integer using normal math round rules (either up or down).

The simple syntax looks like this:

Math.round(number);

Now let us take an example and see how it acutally works. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // creating variables
   float num1 = 3.8f;
   float num2 = 4.5f;

   // applying java math round method
   System.out.println("Rounded value is : "+Math.round(num1));
   System.out.println("Rounded value is : "+Math.round(num2));
   }
}

Output:

Rounded value is : 4
Rounded value is : 5

 

Math.random() method in Java math

The Math.random() method returns a random floating point number between 0 and 1. But if we want a random number between 0 and 100, we can do that by multiplying the out put with 100. The random method in java does not take any parameter.

The simple syntax looks like this.

Math.random();

Now let us take an example and get a random value between 0 and 1 and then get another random number between 0 and 100. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // applying java math random method
   System.out.println("Random value between 0 and 1 : "+Math.random());
   System.out.println("Random value between 0 and 100 : "+(Math.random()*100d));
   }
}

Output:

Random value between 0 and 1 : 0.15870008853930972
Random value between 0 and 100 : 33.288000493538604

Note that we successfully get the random number from specified range.

 

Math.pow() method in Java math

The Math.pow() function takes two parameters and  returns the value of the first parameter raised to the power of the second parameter.

The simple syntax looks like this;

Math.pow(num1, num2);

Now let us take and example and find the power of a number. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // applying java math pow method
   System.out.println("2 raise to the power 4 is   : "+Math.pow(2, 4));
   System.out.println("2.3 raise to the power 4 is : "+Math.pow(2.3, 4));
   }
}  

Output:

2 raise to the power 4 is : 16.0
2.3 raise to the power 4 is : 27.98409999999999

Notice that we can easily find the power of floating numbers as well.

 

Math.sqrt() method in Java math

The Math.sqrt() method calculates the square root of the parameter given to it. It can take integer or floating number.

The simple syntax looks like this;

Math.sqrt(number);

Now let us take an example and see how we can find the the square root of a number. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // applying java math  method
   System.out.println("square root of 9 is  : "+Math.sqrt(9));
   System.out.println("square root of 9.33 is: "+Math.sqrt(9.33));
   }
} 

Output:

square root of 9 is : 3.0
square root of 9.33 is: 3.054504869860253

Notice the the out put is always a floating number.

 

Math.log() method in Java math

The Math.log() method provides the logarithm of the given parameter. The base for the logarithm is i (Euler's number which is equal to 2.71828....).

The simple syntax looks like this:

Math.log(number);

Now let us take an example and see how we can find the log of a number in java programming language. See the example below:

// class
public class Main  { 

   // main method
   public static void main(String[] args)  { 

   // applying java log  method
   System.out.println("log of 3 is  : "+Math.log(3));
   System.out.println("log of 3.454 is : "+Math.log(3.454));
   }
} 

Output:

log of 3 is : 1.0986122886681098
log of 3.454 is : 1.239532979724487

Notice that we can easily find the log of floating numbers as well as shown in the above example.

 

Summary

The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root and many more. In this tutorial, we learned about java math class and java mathematical operators. We look closely at the five different kinds of arithmetic operators that are available in java along with examples. Then we moved to the java math class and explore its various useful methods and solve various examples using those methods. All in all, this tutorial, covers all the information and explanation that you need to know in order to get master in java mathematical operations.

 

Further Reading

Java arithmetic operators
Java math class
more about java math

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment