start page | rating of books | rating of authors | reviews | copyrights

Java Language Reference

Previous Chapter 4
Expressions
Next
 

4.4 Unary Operators

Unary operators are operators that take exactly one argument. Unary operators may appear in a unary expression:

[Graphic: Figure from the text]

The unary plus and minus operators, a Boolean negation operator (!), a bitwise negation operator (~), and the cast construct comprise the unary operators in Java. The unary operators are equal in precedence and are evaluated from right to left.

References Order of Operations; Postfix Increment/Decrement Operators; Prefix Increment/Decrement Operators; Primary Expressions; Type 3

Unary Plus Operator +

The unary plus operator (+) can appear as part of a unary expression. The operator does no explicit computation; it produces the same pure value that is produced by its operand. However, the unary + operator may perform a type conversion on its operand. The type of the operand must be an arithmetic data type, or a compile-time error occurs. If the type of the operand is byte, short, or char, the unary + operator produces an int value; otherwise the operator produces a value of the same type as its operand.

References Arithmetic Types

Unary Minus Operator -

The unary minus operator (-) can appear as part of a unary expression. The type of the operand of the unary - operator must be an arithmetic data type, or a compile-time error occurs. The operator produces a pure value that is the arithmetic negation (i.e., additive inverse) of the value of its operand.

The unary - operator may perform a type conversion. If the type of the operand is byte, short, or char, the operation converts the operand to an int before computing the value's arithmetic negation and producing an int value. Otherwise, unary - produces a value of the same type as its operand.

For integer data types, the unary - operator produces a value equivalent to subtracting its operand from zero. There are, however, negative values for which the unary - operator cannot produce a positive value; in these cases it produces the same negative value as its operand. This behavior results from the two's complement representation Java uses for integer values. The magnitude of the most negative number that can be represented using two's complement notation cannot be represented as a positive number. No exception is thrown when the unary - operator is given a value that cannot be negated. However, you can detect this situation by explicitly testing for these special values. The most negative int value is available as the predefined constant Integer.MIN_VALUE and the most negative long value is available as the predefined constant Long.MIN_VALUE.

For floating-point data types, the unary - operator changes the sign of its operand from + to - or from - to +, for both regular values, positive and negative zero, and positive and negative infinity. The only case where this is not true occurs when the operand is not-a-number (NaN). Given the value NaN, the unary - operator produces NaN.

References Arithmetic Types; Integer; Long

Boolean Negation Operator !

The Boolean negation operator (!) may appear as part of a unary expression. The type of the operand of the ! operator must be boolean, or a compile-time error occurs. If the value of its operand is false, the ! operator produces the pure boolean value true. If the value of its operand is true, the operator produces the pure boolean value false.

Here is an example that uses the Boolean negation operator:

public void paint(Graphics g) {
    if (!loaded) {
        //The next 2 lines are executed if loaded is false
        g.drawString("Loading data", 25, 25);
        return;
    }
    g.drawImage(img, 25, 25, this);
}

References Boolean Type

Bitwise Negation Operator ~

The bitwise negation operator (~) may appear as part of a unary expression. The type of the operand of the ~ operator must be an integer data type, or a compile-time error occurs. The ~ operator may perform a type conversion before it performs its computation. If the type of the operand is byte, short, or char, the operator converts its operand to int before producing a value. Otherwise the ~ operator produces a value of the same type as its operand.

After type conversion, the bitwise negation operator produces a pure value that is the bitwise complement of its operand. In other words, if a bit in the converted operand contains a one, the corresponding bit in the result contains a zero. If a bit in the converted operand contains a zero, the corresponding bit in the result contains a one.

Here's an example that shows the use of the bitwise negation operator:

// zero low order four bits
int getNibble(int x) {
    return x & ~0xf;
}

References Integer types

Casts

A Type enclosed in parentheses specifies a type cast operation. A cast may appear as part of a unary expression. A cast operation always produces a pure value of the specified type, by converting its operand to that type if necessary. This is different from a type cast in C/C++, which can produce garbage if it is given a pointer to a data type different than that implied by the pointer's declaration. If the actual data type of the operand of a cast cannot be guaranteed at compile-time, the Java compiler must produce code to check the type of the operand at runtime. In Java, any value that gets past all of the type-checking done on a cast is guaranteed to be compatible with the type specified by the cast.

A cast can convert between certain primitive types. A cast between object reference types never alters the type or content of the object, but may alter the type of the reference to the object.

Because it is not possible to convert between all types, some cast operations are permitted and others are not. Here are the rules governing casts:

Any cast operation not covered by the preceding rules is not allowed and the Java compiler issues an error message.

References Arithmetic Types; Array Types; Boolean Type; Class Types; Interface Types; Runtime exceptions


Previous Home Next
Increment/Decrement Operators Book Index Multiplicative Operators

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java