Increment and Decrement Operators – Operators – 1Z0-829 Study Guide

Increment and Decrement Operators

Increment and decrement operators, ++ and –, respectively, can be applied to numeric var-iables and have a high order of precedence compared to binary operators. In other words, they are often applied first in an expression.

Increment and decrement operators require special care because the order in which they are attached to their associated variable can make a difference in how an expression is pro-cessed. Table 2.3 lists each of these operators.

TABLE 2.3Increment and decrement operators
OperatorExampleDescription
Pre-increment++wIncreases the value by 1 and returns the new value
Pre-decrement–xDecreases the value by 1 and returns the new value
Post-incrementy++Increases the value by 1 and returns the original value
Post-decrementz–Decreases the value by 1 and returns the original value

The following code snippet illustrates this distinction:

int parkAttendance = 0;
System.out.println(parkAttendance);//0
System.out.println(++parkAttendance);//1
System.out.println(parkAttendance);// 1
System.out.println(parkAttendance);//1
System.out.println(parkAttendance);//0

The first pre-­increment operator updates the value for parkAttendance and outputs the new value of 1. The next post-­decrement operator also updates the value of parkAttendance but outputs the value before the decrement occurs.

For the exam, it is critical that you know the difference between expres-sions like parkAttendance++ and ++parkAttendance. The increment and decrement operators will be in multiple questions, and confusion about which value is returned could cause you to lose a lot of points on the exam.

Working with Binary Arithmetic Operators

Next, we move on to operators that take two operands, called binary operators. Binary operators are by far the most common operators in the Java language. They can be used to perform mathematical operations on variables, create logical expressions, and perform basic variable assignments. Binary operators are often combined in complex expressions with other binary operators; therefore, operator precedence is very important in evaluating expressions containing binary operators. In this section, we start with binary arithmetic operators; we expand to other binary operators in later sections.

Arithmetic Operators

Arithmetic operators are those that operate on numeric values. They are shown in Table 2.4.

TABLE 2.4Binary arithmetic operators
OperatorExampleDescription
Additiona + bAdds two numeric values
Subtractionc- dSubtracts two numeric values
Multiplicatione * fMultiplies two numeric values
Divisiong / hDivides one numeric value by another
Modulusi % jReturns the remainder after division of one numeric value by
another

You should know all but modulus from early mathematics. If you don’t know what modulus is, though, don’t worry—­we’ll cover that shortly. Arithmetic operators also include the unary operators, ++ and –, which we covered already. As you may have noticed in Table 2.1, the multiplicative operators (*, /, %) have a higher order of precedence than the additive operators (+, -­). Take a look at the following expression:

int price = 2 * 5 + 3 * 4 8;

First, you evaluate the 2 * 5 and 3 * 4, which reduces the expression to this:

int price = 10 + 12 8;

Then, you evaluate the remaining terms in left-­to-­right order, resulting in a value of price of 14. Make sure you understand why the result is 14 because you will likely see this kind of operator precedence question on the exam.

All of the arithmetic operators may be applied to any Java primitives, with the exception of boolean. Furthermore, only the addition opera-tors + and += may be applied to String values, which results in String concatenation. You will learn more about these operators and how they apply to String values in Chapter 4, “Core APIs.”