Operators

0




Operators

This section describes the operators in the Java programming language.

Operator Precedence

The Java programming language operators are similar in style and function to those of C and C++. Table 4-2 lists the operators in order of precedence (L to R means left-to-right associative; R to L means right-to-left associative).

t1

Logical Operators

Most Java technology operators are taken from other languages and behave as expected. Relational and logical operators return a boolean result. The value 0 is not interpreted automatically as false and non-zero values are not interpreted automatically as true.

int i = 1;

if (i) // generates a compile error

if (i != 0) // Correct

The boolean operators supported are !, &, ^, and | for the algebraic Boolean operations NOT, AND, XOR, and OR, respectively. Each of these returns a boolean result. The operators && and || are the short-circuit equivalents of the operators & and |.

Short-Circuit Logical Operators

The operators && (defined as AND) and || (defined as OR) perform short-circuit logical expressions. Consider this example:

MyDate d = reservation.getDepartureDate();

if ((d != null) && (d.day > 31)) {

// do something with d

}

The boolean expression that forms the argument to the if () statement is legal and entirely safe. This is because the second subexpression is skipped when the first subexpression is false, because the entire expression is always false when the first subexpression is false, regardless of how the second subexpression evaluates. Similarly, if the || operator is used and the first expression returns true, the second expression is not evaluated because the whole expression is already known to be true.

Bitwise Logical Operators

Bit manipulation operations, including logical and shift operations, perform low-level operations directly on the binary representations used in integers. These operations are not often used in enterprise-type systems but might be critical in graphical, scientific, or control systems. The ability to operate directly on binary might save large amounts of memory, might enable certain computations to be performed very efficiently, and can greatly simplify operations on collections of bits, such as data read from or written to parallel I/O ports. The Java programming language supports bitwise operations on integral data types. These are represented as the operators ~, &, ^, and | for the bitwise operations of NOT (bitwise complement), bitwise AND, bitwise XOR, and bitwise OR, respectively.

Figure shows examples of the bitwise operators on byte-sized binary numbers.

t2

Right-Shift Operators >> and >>>

The Java programming language provides two right-shift operators. The operator >> performs an arithmetic or signed right shift. The result of this shift is that the first operand is divided by 2 raised to the number of times specified by the second operand. For example:

128 >> 1 returns 128/21 = 64

256 >> 4 returns 256/24 = 16

-256 >> 4 returns -256/24 = -16

The >> operator results in the sign bit being copied during the shift. The logical or unsigned right shift operator >>> works on the bit pattern rather than the arithmetic meaning of a value and always places 0s in the most significant bits; for example:

1010 … >> 2 gives 111010 …

1010 … >>> 2 gives 001010 …

Left-Shift Operator <<

The operator << performs a left shift. The result of this shift is that the first operand is multiplied by two raised to the number specified by the second operand; for example:

128 << 1 returns 128*21 = 256

16 << 2 returns 16*22 = 64

All three shift operators reduce their right-hand operand modulo 32 for an int type left-hand operand and modulo 64 for a long type left-hand operand. Therefore, for any int x, x >>> 32 results in x being unchanged, not 0 as you might expect. The shift operators are permitted only on integral types. The unsigned right shift >>> is effective only on int or long values. If you use it on a short or byte value, the value is promoted, with sign extension, to an int before >>> is applied. By this point, the unsigned shift usually has become a signed shift.

Shift Operator Examples

Figure show the bit patterns of a positive and a negative number and the bit patterns resulting from the three shift operators: >>, >>>, and <<.

t3

String Concatenation With +

The + operator performs a concatenation of String objects, producing a new String.

String salutation = “Dr. “;

String name = “Pete” + ” ” + “Seymour”;

String title = salutation + ” ” + name;

The result of the last line is:

Dr. Pete Seymour

If either argument of the + operator is a String object, then the other argument is converted to a String object. All objects can be converted to a String object automatically, although the result might be rather cryptic. The object that is not a String object is converted to a string equivalent using the toString() member function.