Java Tutorial: Math Class

Every Tuesday I take some time to put together a Java tutorial.  You will notice at the top there is a page indexing them all.  Enjoy!

So turns out, I forgot yesterday was Tuesday so this Java tutorial is a day late!  Since I don’t have to many readers yet, that’s okay I guess.  So today I am going to go over the math class, and also some additional resources.  I wont be touching on all of the methods of the Math class in this tutorial, but some that might be useful, and at the bottom I am going to link off to the documentation of the Math class on Oracles website.

Because Math is in the java.lang package, it does not need to be explicitly imported.  This means there is no need for an import statement, so we will start off our program the same way we have been.  Here is a generic way of showing how to utilize the Math class.


Pretty simple right?  Just make sure that Math has a capital M and there is a . between Math and whichever method of Math you are using.  Since we have not gone over methods in detail I will go over that once I show some of the useful methods of Math, and go from there.

Math.abs(some numeric value);   <----returns the absolute value of a number.

Math.exp(double);               <-----returns the constant e raised to the double.

Math.log(double);               <-----returns the natural log of the double.

Math.max(value, value);         <------returns the max of two numbers (type int, float or double).

Math.min(value, value);         <------returns the min of two numbers (type int, float or double).

Math.pow(double, double);       <-------returns the value of the first argument raised to the second argument.

Now this is just a sample of the different methods the Math class has.  To see the full list go here.  Now onto methods.  What is a method?  I like to describe it as an action a class can perform.  Math being the class, each of these things after the . are the actions that Math can perform.  So you want to find the absolute value of a number?  Pass the number as an  argument to the method.  This is easier to show than tell:




The first example shows that the argument is passed inside the parenthesis to the method, and the second example shows you how that might look.  You can also pass variables, and not just literals.  Just check the documentation to make sure the data type of the variable you are trying to pass matches the data type the method is expecting.  In the case of the Math class, it is usually just a number of some type (float, double, int) and usually not a String.

If you have any questions or need clarification, please leave a comment and I can get that sorted out for you!

Edit: Another way to use the Math class is as follows, to save time!

import static java.lang.Math.*;

public class MathTest
    public static void main(String[] args)

As you can see, there is a different way of importing.  While you don’t need to import the Math class for it to work, this will keep you from having to keep using Math.method() every time.  All you need to put in your file is the following:

import static java.lang.Math.*;

Once you have that, you can call the methods as if they were functions.  In the example above, you see that I am using the abs method of the Math class to find the absolute value of -12.  This works for all of the methods.

Thanks Phoxis for pointing this edit out!


Practice Problem

Make a program that asks the user for 5 numbers, and it will determine which is the maximum number, and which is the minimum number.

Also try using all of the different Math methods, and try passing it arguments through literals AND variables.  Have fun!


2 responses to “Java Tutorial: Math Class”

  1. phoxis says :

    Why don’y you static import the Math class and avoid prepending the math functions with the “Math.” repeatedly. Save both code size and your sweat.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: