For the exam, you also need to know about the bitwise complement operator (~
), which flips all of the 0
s and 1
s in a number. It can only be applied to integer numeric types such as byte
, short
, char
, int
, and long
. Let's try an example. For simplicity, we only show the last four bits (instead of all 32 bits).
int value = 3; // Stored as 0011 int complement = ~value; // Stored as 1100 System.out.println(value); // 3 System.out.println(complement); // -4
Relax! You don't need to know how to do complicated bit arithmetic on the exam, as long as you remember this rule: to find the bitwise complement of a number, multiply it by negative one and then subtract one.
System.out.println(-1*value - 1); // -4 System.out.println(-1*complement - 1); // 3
Moving on to more common operators, the negation operator (-
) reverses the sign of a numeric expression, as shown in these statements:
double zooTemperature = 1.21; System.out.println(zooTemperature); // 1.21 zooTemperature = -zooTemperature; System.out.println(zooTemperature); // -1.21 zooTemperature = -(-zooTemperature); System.out.println(zooTemperature); // -1.21
Notice that in the last example we used parentheses, ()
, for the negation operator, -
, to apply the negation twice. If we had instead written --
, then it would have been interpreted as the decrement operator and printed -2.21
. You will see more of that decrement operator shortly.
Based on the description, it might be obvious that some operators require the variable or expression they're acting on to be of a specific type. For example, you cannot apply a negation operator (-
) to a boolean
expression, nor can you apply a logical complement operator (!
) to a numeric expression. Be wary of questions on the exam that try to do this, as they cause the code to fail to compile. For example, none of the following lines of code will compile:
int pelican = !5; // DOES NOT COMPILE boolean penguin = -true; // DOES NOT COMPILE boolean peacock = !0; // DOES NOT COMPILE
The first statement will not compile because in Java you cannot perform a logical inversion of a numeric value. The second statement does not compile because you cannot numerically negate a boolean
value; you need to use the logical inverse operator. Finally, the last statement does not compile because you cannot take the logical complement of a numeric value, nor can you assign an integer to a boolean
variable.
0
or 1
) with boolean
expressions. Unlike in some other programming languages, in Java, 1
and true
are not related in any way, just as 0
and false
are not related.
Increment and Decrement Operators
Increment and decrement operators, ++
and --
, respectively, can be applied to numeric variables 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 processed. Table 2.3 lists each of these operators.
TABLE 2.3 Increment and decrement operators
Operator | Example | Description |
---|---|---|
Pre-increment |
++w
|
Increases the value by 1 and returns the new value |
Pre-decrement |
--x
|
Decreases the value by 1 and returns the new value |
Post-increment |
y++
|
Increases the value by 1 and returns the original value |
Post-decrement |
z--
|
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.
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.4 Binary arithmetic operators
Operator | Example | Description |
---|---|---|
Addition |
a + b
|
Adds two numeric values |
Subtraction |
c - d
|
Subtracts two numeric values |
Multiplication |
e * f
|
Multiplies two numeric values |
Division |
|