OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
b -= 0.2
c *= 100
d /= 4
Compound operators are really just glorified forms of the simple assignment operator, with a built-in arithmetic or logical operation that applies the left and right sides of the statement and stores the resulting value in the variable on the left side of the statement. For example, the following two statements after the declaration of camel
and giraffe
are equivalent when run independently:
int camel = 2, giraffe = 3; camel = camel * giraffe; // Simple assignment operator camel *= giraffe; // Compound assignment operator
The left side of the compound operator can be applied only to a variable that is already defined and cannot be used to declare a new variable. In this example, if camel
were not already defined, the expression camel *= giraffe
would not compile.
Compound operators are useful for more than just shorthand—they can also save you from having to explicitly cast a value. For example, consider the following. Can you figure out why the last line does not compile?
long goat = 10; int sheep = 5; sheep = sheep * goat; // DOES NOT COMPILE
From the previous section, you should be able to spot the problem in the last line. We are trying to assign a long
value to an int
variable. This last line could be fixed with an explicit cast to (int)
, but there's a better way using the compound assignment operator:
long goat = 10; int sheep = 5; sheep *= goat;
The compound operator will first cast sheep
to a long
, apply the multiplication of two long
values, and then cast the result to an int
. Unlike the previous example, in which the compiler reported an error, the compiler will automatically cast the resulting value to the data type of the value on the left side of the compound operator.
Return Value of Assignment Operators
One final thing to know about assignment operators is that the result of an assignment is an expression in and of itself equal to the value of the assignment. For example, the following snippet of code is perfectly valid, if a little odd-looking:
long wolf = 5; long coyote = (wolf=3); System.out.println(wolf); // 3 System.out.println(coyote); // 3
The key here is that (wolf=3)
does two things. First, it sets the value of the variable wolf
to be 3
. Second, it returns a value of the assignment, which is also 3
.
The exam creators are fond of inserting the assignment operator (=
) in the middle of an expression and using the value of the assignment as part of a more complex expression. For example, don't be surprised if you see an if
statement on the exam similar to the following:
boolean healthy = false; if(healthy = true) System.out.print("Good!");
While this may look like a test if healthy
is true
, it's actually assigning healthy
a value of true
. The result of the assignment is the value of the assignment, which is true
, resulting in this snippet printing Good!
. We'll cover this in more detail in the upcoming “Equality Operators” section.
Comparing Values
The last set of binary operators revolves around comparing values. They can be used to check if two values are the same, check if one numeric value is less than or greater than another, and perform Boolean arithmetic. Chances are, you have used many of the operators in this section in your development experience.
Equality Operators
Determining equality in Java can be a nontrivial endeavor as there's a semantic difference between “two objects are the same” and “two objects are equivalent.” It is further complicated by the fact that for numeric and boolean
primitives, there is no such distinction.
Table 2.7 lists the equality operators. The equals operator (==
) and not equals operator (!=
) compare two operands and return a boolean
value determining whether the expressions or values are equal or not equal, respectively.
TABLE 2.7 Equality operators
Operator | Example | Apply to primitives | Apply to objects |
---|---|---|---|
Equality |
a == 10
|
Returns true if the two values represent the same value
|
Returns true if the two values reference the same object
|
Inequality |
b != 3.14
|
Returns true if the two values represent different values
|
Returns true if the two values do not reference the same object
|
The equality operator can be applied to numeric values, boolean
values, and objects (including String
and null
). When applying the equality operator, you cannot mix these types. Each of the following results in a compiler error:
boolean monkey = true == 3; // DOES NOT COMPILE boolean ape = false != "Grape"; // DOES NOT COMPILE boolean gorilla = 10.2 == "Koko"; // DOES NOT COMPILE
Pay close attention to the data types when you see an equality operator on the exam. As mentioned in the previous section, the exam creators also have a habit of mixing assignment operators and equality operators.
boolean bear = false; boolean polar = (bear = true); System.out.println(polar); // true
At first glance, you might think the output should be false
, and if the expression were (bear == true)
, then you would be correct. In this example, though, the expression is assigning the value of true
to bear
, and as you saw in the section on assignment operators, the assignment itself has the value of the assignment. Therefore, polar
is also assigned a value of true
, and the output is true
.
For object comparison, the equality operator is applied to the references to the objects, not the objects they point to. Two references are equal if and only if they point to the same object or both point to null
. Let's take a look at some examples:
var monday = new File("schedule.txt"); var tuesday = new File("schedule.txt"); var wednesday = tuesday; System.out.println(monday == tuesday); // false System.out.println(tuesday == wednesday); // true
Even though all of the variables point to the same file information, only two references, tuesday
and wednesday
, are equal in terms of ==
since they point to the same object.