OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky. Читать онлайн. Newlib. NEWLIB.NET

Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Программы
Год издания: 0
isbn: 9781119864592
Скачать книгу
b -= 0.2 Subtracts the value on the right from the variable on the left and assigns the difference to the variable Multiplication assignment c *= 100 Multiplies the value on the right with the variable on the left and assigns the product to the variable Division assignment d /= 4 Divides the variable on the left by the value on the right and assigns the quotient to the variable

      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

      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!");

      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.

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.

      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.

       Скачать книгу