Please enable JavaScript to use CodeHS

Basic Math in Java

By Evelyn Hunter

Being able to manipulate numbers and perform mathematical operations is an essential skill in Java.

The nice thing about the math in Java is that we approach it exactly the same way we would if we were doing it by hand or with a calculator. The following operations are the same on a calculator as they are in our Java programs:

+ for Addition
- for Subtraction
/ for Division
* for Multiplication

The following program demonstrates each of these operations in action. Run the program to see the results!

Types of Division

You may have noticed in the previous example that the result of 45 / 23 was incorrect. This happens because both values in the equation were of type int. When two ints are used in an equation, the resulting value will always be an int. As a result, division with int values may produce inaccurate results, depending on the type of information you are looking to gather.

If a programmer is looking for the closest whole number to a value, such as, how many people can fit in a room, then using int division may make the most sense - we can’t have one half of a person in the room! If they need a more precise calculation, then their calculation must include a double value. A double divided by an int, or a double divided by another double will always result in a double!

In addition to the traditional operations, Java also uses the modulus operator to make calculations. Modulus returns the remainder of two values:

5 % 2 = 1

When looking for the remainder, we need to figure out the greatest whole number that can be divided by the two values. In this case, the greatest whole number is 4, since 4/2 is 2. As a result, the remainder of this program is 1 (5 - 4 = 1), indicating that there is one number leftover after 2 is divided as best as possible by 5.

The modulus operator is particularly useful when trying to determine if a number is even or not. In the above example, any number %2 that results in a remainder of 1 is odd, and any number that results in a remainder of 0 is even.

Operation Precedence

The order of operations that calculations are executed still applies in Java. The operations in a calculation will follow the order of Parentheses, then Exponents, then Multiplication and Division, then Addition and Subtraction:

Operator
    Precedence
( ) Parenthesis
    1st
* / Multiplication and Division
    2nd
+ - Addition and Subtraction
    3rd

In instances where there are multiple operators with the same precedence, the order will be left to right.

See if you can answer the following quiz questions related to order of operations!

  1. Incorrect Correct No Answer was selected Invalid Answer

    Choose the correct output of the code: System.out.println(5 + (7 - 2) * 2).

  2. Incorrect Correct No Answer was selected Invalid Answer

    What’s the result of 20 % 7?

How’d you do?

Now that you’ve got a handle on arithmetic expressions, let’s try a practice problem.

Practice

A movie theater sells popcorn in two different sizes, small (5 ounces) and large (8 ounces). A big group comes to the theater and decides they want to buy all the popcorn. They want as many large bags as possible, then they want to purchase the rest as small bags. If a small bag costs $3, and a large bag costs $5, and the total amount of popcorn available is 91 ounces, how much money will the movie theater charge the group, and how much popcorn is leftover?

Use the following program to write out your solution: