Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 154

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.

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

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх