Java Reference
In-Depth Information
Both print statements print “true” in the console. After all, a student is a person. The nature of
object-oriented programming, where one class always extends another, demands that the instanceof
operator works this way.
The instanceof operator can cause subtle problems. In particular, the instanceof operator won't
throw an error if its right-hand operand is null . Instead, it always returns false in those cases. If you use
something like myClass instanceof myBaseClass and have inadvertently set myBaseClass to null , the
result is false . You might then think that myClass isn't an instance of myBaseClass , but you can't actually
know that if you're comparing to null . This is another great place to practice defensive programming
and make sure you're not getting null there.
If you're careful to make sure you know the relationships between the classes you use (which is part
of being a good object-oriented programmer) and to ensure that you're not comparing to null , you
shouldn't get into too much trouble with the instanceof operator.
Equality Operators
Java has two equality operators: == (equals) and != (not equals). (The exclamation point, both in this
context and in others, is sometimes called a “bang” by software developers.) The equality operator ( == )
and the inequality operator ( != )work the same way (each is the negative of the other), so I'll confine this
discussion to the equality operator.
Java uses a single equal sign as the base assignment operator (we get to the assignment operators
later in this chapter), so it can't use a single equal sign for comparisons. Consequently, Java (and the
other languages that share Java's basic syntax) uses two equal signs.
A common mistake among folks new to Java (and not-so-new folks who aren't careful with their
typing) is to use a single equal sign (the assignment operator) when they should use two equal signs (the
equality operator) and accidentally assign a value to a variable rather than compare the variable's value
to something else. Consider the following example in Listing 4-14:
Listing 4-14. Accidental assignment
for (int counter = 0; counter < 10; counter++) {
if (counter = 0) { // should be == rather than =
// do something
}
}
That bit of code, if not corrected, causes an infinite loop. In every iteration, counter gets set to 0, so
its value is always less than 10. Fortunately, Eclipse and other modern development tools warn you
when you do this. You can still do it, but at least you do it intentionally (though you really should re-
structure your code if you need to reset a value). I have to admit that I have gotten into exactly that
situation more than once (because of poor typing), though never since I started using Eclipse, thanks to
its warnings. That's one of the reasons I had you install Eclipse at the start of this journey.
For primitives, the equality operators work exactly as you probably think they would. However, for
objects, the equality operators indicate whether two references refer to the same instance. As ever,
examples help (see Listing 4-15).
Search WWH ::




Custom Search