# Operators¶

Java uses the standard mathematical operators for addition (+), subtraction (-), multiplication (*), and division (/). Java uses (==) to test if the value on the left is equal to the value on the right and (!=) to test if two items are not equal.

Do all of those operators do what you expected? What about 2 / 3 prints 0?

Note

When Java sees you doing integer division it assumes you want an integer result so it throws away anything after the decimal point in the answer.

## Modulus¶

The percent sign operator (%) is the modulus or remainder operator. The modulus operator (x % y) returns the remainder after you divide x (first number) by y (second number) so 5 % 2 will return 1 since 2 goes into 5 two times with a remainder of 1. Remember long division when you had to specify how many times one number when into another evenly and the remainder? That remainder is what is returned by the modulus operator.

Figure 1: Long division showing the whole number result and the remainder

Note

The result of x % y when x is smaller than y is always x. The value y can’t go into x at all (goes in 0 times), since x is smaller than y, so the result is just x. So if you see 2 % 3 the result is 2.

3-4-1: What is the result of 158 % 10?
• (A) 15
• This would be the result of 158 divided by 10. Modulus gives you the remainder.
• (B) 16
• Modulus gives you the remainder after the division.
• (C) 8
• When you divide 158 by 10 you get a remainder of 8.
3-4-2: What is the result of 3 % 8?
• (A) 3
• 8 goes into 3 no times so the remainder is 3. The remainder of a smaller number divided by a larger number is always the smaller number!
• (B) 2
• This would be the remainder if the question was 8 % 3 but here we are asking for the reminder after we divide 3 by 8.
• (C) 8
• What is the remainder after you divide 3 by 8?

## Modulus on Past Exams¶

The modulus operator has been used quite a bit on the AP CS A exam, so you should be familiar with it.

• Use it to check for odd or even numbers (num % 2 == 1 is odd and num % 2 == 0 is even). Actually, you can use it to check if any number is evenly divisible by another (num1 % num2 == 0)
• Use it to get the last digit from an integer number (num % 10 = last digit on right). This approach could be used on the free response question Self Divisor (Question 1 from 2007). See http://coweb.cc.gatech.edu/ice-gt/1277 for starter code and testing code.
• Use it to get the number of minutes left when you convert to hours (num % 60). Also whenever you have limited storage and you need to wrap around to the front if the value goes over the limit. See question 3 at http://coweb.cc.gatech.edu/ice-gt/1278.

## Shortcut Operators¶

You are also expected to know the double plus operator (++) and the double minus operator (--). The ++ operator is used to add one to the current value: x++ is the same as x = x + 1. The -- operator is used to subtract one from the current value: y-- is the same as y = y - 1. You should know that x += y is the same as x = x + y, x -= y is the same as x = x - y, x *= y is the same as x = x * y, and x /= y is the same as x = x / y.

Note

On the exam you can use x++ or ++x to both add one to the value of x. These two shortcuts only have different results if you assign the value of x to another variable as in int y = ++x; or int y = x++;. In int y = ++x; the value of x would be incremented before y’s value is set to a copy of x’s value. In int y = x++; the value of y would be set to a copy of x’s value before x is incremented. The exam will never use a shortcut in an assignment statement, so you don’t need to worry about the difference between ++x or x++.

3-4-3: What are the values of x, y, and z after the following code executes?

int x = 0;
int y = 1;
int z = 2;
x--; // x followed bythe double minus sign
y++;
z+=y;

• (A) x = -1, y = 1, z = 4
• This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
• (B) x = -1, y = 2, z = 3
• This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
• (C) x = -1, y = 2, z = 2
• This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
• (D) x = -1, y = 2, z = 2
• This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
• (E) x = -1, y = 2, z = 4
• This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.

3-4-4: What are the values of x, y, and z after the following code executes?

int x = 3;
int y = 5;
int z = 2;
x = z * 2;
y = y / 2;
z++;

• (A) x = 6, y = 2.5, z = 2
• This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
• (B) x = 4, y = 2.5, z = 2
• This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
• (C) x = 6, y = 2, z = 3
• This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
• (D) x = 4, y = 2.5, z = 3
• This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
• (E) x = 4, y = 2, z = 3
• This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
Next Section - Casting Variables