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.
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 compli-cated 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
Equality | a == | 10 | Returns true if the two values |
represent the same value | |||
Inequality | b != | 3.14 | Returns true if the two values |
represent different values |
Returns true if the two values ref-erence the same object
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.
Wait, what’s the File class? In this example, as well as during the exam, you may be presented with class names that are unfamiliar, such as File. Many times you can answer questions about these classes without know-
ing the specific details of these classes. In the previous example, you should be able to answer questions that indicate monday and tuesday are two separate and distinct objects because the new keyword is used, even if you are not familiar with the data types of these objects.
In some languages, comparing null with any other value is always false, although this is not the case in Java.
System.out.print(null == null); // true
In Chapter 4, we’ll continue the discussion of object equality by introducing what it means for two different objects to be equivalent. We’ll also cover String equality and show how this can be a nontrivial topic.