Java: Arithmetic Operators

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

A large chunk of programming involves math, from addition to multiplication to modulo operations. These math operations need not be confusing or overly complex. This lesson will cover the arithmetic operators in Java and provide working code examples.

Who Put the Math in Java?

Programming is math, no matter how you look at it. We are constantly adding, subtracting, multiplying and dividing values. Arithmetic is a really a key component of any programing language.

Java provides the four basic math operators, plus an additional one we call modulo operations. We'll get to that in a minute. It is a powerful little tool in its own right.

The Basics

Operator Function Example
+ Addition input1 + input2 + input3
- Subtraction sales - tax_amount
* Multiplication sales * commission
/ Division hours / minutes
% Modulo Returns a remainder of division, e.g., input % 2

Addition and Subtraction

Addition and subtraction use the + and - operators, as you find on any calculator. The following Java code shows the adding of a tax amount to a rate, followed by the subsequent subtraction of that rate. We'll set a value to hold the total, then subtract the rate from that total. If we do it right, we'll arrive back at the original rate:

//Our numbers:
double rate = 10.87;
double taxAmount = 3.25;
double newValue;
//Let's do some addition!
newValue = rate + taxAmount;
//Now subtract it!
System.out.println(newValue - taxAmount);

The output of this operation:

Java Add Subtract Output

Note that Java will perform the math operations in order. For example, if you subtract a larger number from a smaller number, a negative value will result. Java follows the same order of operations as math does in the real world!

Multiplication and Division

Multiplication uses the asterisk (*) and division the forward slash (/) for their respective operations. Staying with our amount and tax rate example, let's multiply the amount by a tax rate, say .05. We'll use a large amount to simulate an annual tax bill. First we will multiply, then subtract the amount from the total earnings.

//Our numbers:
double earnings = 36504.87;
double taxRate = 0.05;
double taxBill;
double netPay;
//Multiply to find annual tax
taxBill = (earnings * taxRate);
netPay = earnings - taxBill;
System.out.println("Tax Bill: " + taxBill);
System.out.println("Net Pay: " + netPay);

This is the output. For now, we'll just show the whole number, rounding is another lesson.

Java math multiply output

This could have all been done in one statement, but when you are first starting out with programming, it is often best to keep the statements simple. Once you know they work, you can start to combine to reduce code.

Continuing with our example, let's divide the tax amount by 12, which will give us a monthly payment amount. We will repeat the entire block of code from above, in order to put it all together:

//Our numbers:
double earnings = 36504.87;
double taxRate = 0.05;
double taxBill;
double netPay;
//Multiply to find annual tax
taxBill = (earnings * taxRate);
netPay = earnings - taxBill;
System.out.println("Tax Bill: " + taxBill);
System.out.println("Net Pay: " + netPay);
// Divide by 12
System.out.println("Monthly Amount: " + taxBill / 12);

The output, with all values:

Java divide output

Be careful with multiplication, especially with data types. Notice that we've used the data type that holds the largest values, double. If you are dividing or multiplying, use double for the greatest precision!

A Note on Combining Operations

It is perfectly legal to combine multiple operations, just as you would in math. Remember that the order of operations rules still apply! This means that you need to make sure your parentheses all line up so you get your intended answer.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account