This book is now obsolete Please use CSAwesome instead.

3.5. 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.

3.5.1. Modulo

The percent sign operator (%) is the modulo or remainder operator. The modulo 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 modulo operator.

../_images/mod-py.png

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.

Check Your Understanding

3.5.2. Modulo on Past Exams

The modulo 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.

3.5.3. 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++.

You have attempted of activities on this page