In the last couple of lessons, we have been going over different operator symbols to use for making our desired math equation. These operators have been:

Addition +

Subtraction –

Multiplication *

Division /

However, what if we want to have more complex equations? Just like in algebra, we are going to be using parenthesis!

If you have taken algebra, you probably remember learning the order of operations. If not, all you need to remember is PEDMAS. This stands for:

Parenthesis

Exponents

Multiplication

Division

Addition

Subtraction

All this does is help us remember what order to execute a problem when there are multiple steps within our math problem. In order to help cement this idea, let’s make a program in Java.

First, make a class called ParenthesisExample1 and put the Java main method within it.

Then put the Java println() method inside of our main method.

Next, let us put the following inside math equation inside of our print method: 18-6 / 3

After compiling and running our code, we get 16. This isn’t quite the answer we want, but the computer is merely doing what it has been told and utilizing the PEDMAS order of operations.

Now, lets change our equation to enclose the numerator (18 – 6) and put it in parenthesis. It should look like this:

Now we get the desired output of 4.

Great job! You should now have a basic grasp of how to utilize basic math operators in Java. Although Java has a lot more to offer than PEDMAS, it is certainly the most important to master.

Also, remember that although we learned that parenthesis can be used for math equations, that they can also be utilized for methods – such as the parenthesis used with the System.out.println() method.