Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 4 ■ OPERATORS



  • Using a shift operator on byte, char, or short values promotes those values to int
    values. Unary numeric promotion strikes again. It's unary because both operands
    are separately promoted before the operation is performed. So, a byte value
    shifted by another byte value leads to two separate unary promotions before the
    shift operation happens. Sometimes, that doesn’t matter. Other times, you might
    need to account for the promotion when assigning the result of the shift to
    another variable.

  • Signed shifting causes trouble with negative values. In the ShiftDemo example, I
    didn't use the signed shift operators (<< and >>) because doing so on that value
    produces an exception. That happens because of the way Java stores negative
    numbers; shifting the bits that comprise a negative number results in a binary
    string that Java can't recognize as any number.

  • Integer.toBinaryString shows the minimum possible number of digits. In many
    of the values shown by the ShiftDemo results, the internal representation is 32 bits
    long, with all the bits to the left of the value shown being 0 (that is, those values are
    0-padded). That's why the binary strings for the unsigned right shift operator are
    32 bits long.

  • When used on a relatively small negative number, the unsigned shift operator can
    create a large positive number. That happens because the result of the operation is
    a string of binary digits that the JVM can recognize as a number. However, that
    large number might not be what you have in mind. And that takes us to our last
    point:

  • Shifting is tricky. Test very carefully when you use the shift operators.


So why use the shift operators? Because they are the fastest possible operators. The simplest and
fastest operation a computer can do is to shift a binary value. If you can be sure that shifting produces
the values you want or if you happen to be working with values that aren't really values but rather
collections of switches (such as graphics settings), the shift operators can provide efficient ways to
manipulate those values or switches. We cover the idea of using a value as a series of switches when we
get to the bitwise operators, later in this chapter.

Relational Operators


The relational operators compare things to one another. In particular, they determine whether one value
is greater than, less than, equal to, or not equal to another value. For the relational operators to work, the
items being compared have to be comparable. That sounds obvious, but it has a particular meaning in
Java. The language specification defines what it means to be comparable for the primitives. Thus, you
can compare an int to a float and get a meaningful result (the JVM promotes the int to a float and
then does the comparison). So long as one or the other can be cast to the other value, Java can
meaningfully compare primitives, and Eclipse tells you when they can't be.
> (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to) all work on
primitives, but they don't work on objects. Conversely, instanceof works on objects but not on
primitives. Java does provide ways to compare objects to one another, but not through any operators.
We get to comparing objects later in this chapter.
Free download pdf