Invalid instanceof – Working with Binary Arithmetic Operators – 1Z0-829 Study Guide

Invalid instanceof

One area the exam might try to trip you up on is using instanceof with incompatible types. For example, Number cannot possibly hold a String value, so the following causes a compilation error:

public void openZoo(Number time) {

if(time instanceof String) // DOES NOT COMPILE System.out.print(time);

}

If the compiler can determine that a variable cannot possibly be cast to a specific class, it reports an error.

null and the instanceof operator

What happens if you call instanceof on a null variable? For the exam, you should know that calling instanceof on the null literal or a null reference always returns false.

System.out.print(null instanceof Object); // false

Object noObjectHere = null;

System.out.print(noObjectHere instanceof String); // false

The preceding examples both print false. It almost doesn’t matter what the right side of the expression is. We say “almost” because there are exceptions. This example does not com-pile, since null is used on the right side of the instanceof operator:

System.out.print(null instanceof null); // DOES NOT COMPILE

Although it may feel like you’ve learned everything there is about the instanceof operator, there’s a lot more coming! In Chapter 3, we intro-duce pattern matching with the instanceof operator, which was offi-cially added in Java 16. In Chapter 7, “Beyond Classes,” we introduce

polymorphism in much more detail and show how to apply these rules to interfaces.

Logical Operators

If you have studied computer science, you may have already come across logical operators before. If not, no need to panic—­we’ll be covering them in detail in this section.

The logical operators, (&), (|), and (^), may be applied to both numeric and boolean data types; they are listed in Table 2.9. When they’re applied to boolean data types, they’re referred to as logical operators. Alternatively, when they’re applied to numeric data types, they’re referred to as bitwise operators, as they perform bitwise comparisons of the bits that compose the number. For the exam, though, you don’t need to know anything about numeric bitwise comparisons, so we’ll leave that educational aspect to other books.

TABLE 2 . 9 Logical operators

OperatorExampleDescription
Logical ANDa & bValue is true only if both values are true.
Logical inclusive ORc |dValue is true if at least one of the values is true.
Logical exclusive ORe ^fValue is true only if one value is true and the other is false.

You should familiarize yourself with the truth tables in Figure 2.2, where x and y are assumed to be boolean data types.

FIGURE 2 . 2 The logical truth tables for &, |, and ^

AND (x & y) INCLUSIVE OR (x | y) EXCLUSIVE OR (x ^ y)
y =y = y =y = y =y =
truefalse truefalse truefalse
x =truefalse x =truetrue x =falsetrue
true true true
x =falsefalse x =truefalse x =truefalse
false false false

Here are some tips to help you remember this table:

  • AND is only true if both operands are true.
  • Inclusive OR is only false if both operands are false.
  • Exclusive OR is only true if the operands are different. Let’s take a look at some examples:

boolean eyesClosed = true;

boolean breathingSlowly = true;

boolean resting = eyesClosed | breathingSlowly; boolean asleep = eyesClosed & breathingSlowly; boolean awake = eyesClosed ^ breathingSlowly;

System.out.println(resting);// true
System.out.println(asleep);//true
System.out.println(awake);//false

You should try these out yourself, changing the values of eyesClosed and breathingSlowly and studying the results.