Next Prev Up Contents

## 7 Expressions

7.1 - Operators
7.1.1 - Operators on Integers
7.1.2 - Operators on Boolean Values
7.1.3 - Operators on Floating Point Values
7.1.4 - Operators on Arrays
7.1.5 - Operators on Strings
7.1.6 - Operators on Objects
7.2 - Casts and Conversions

Expressions in the language are much like expressions in C.

### 7.1 Operators

The operators, from highest to lowest precedence, are:

    . [] ()
    ++ -- ! ~ instanceof
    * / %
    + -
    << >> >>>
    < > <= >=
    == !=
    &
    ^
    |
    &&
    ||
    ?:
    = op=
    ,

#### 7.1.1 Operators on Integers

For operators with integer results, if any operand is long, the result type is long. Otherwise the result type is int--never byte, short, or char. Thus, if a variable i is declared a short or a byte, i+1 would be an int. When a result outside an operator's range would be produced, the result is reduced modulo the range of the result type.

The unary integer operators are:

The ++ operator is used to express incrementing directly. Incrementing can also be expressed indirectly using addition and assignment. ++lvalue means lvalue+=1. ++lvalue also means lvalue=lvalue+1 (as long as lvalue has no side effects). The -- operator is used to express decrementing. The ++ and -- operators can be used as both prefix and postfix operators.

The binary integer operators are:

Integer division rounds toward zero. Division and modulus obey the identity (a/b)*b + (a%b) == a.

The only exceptions for integer arithmetic are caused by a divide or modulus by zero, which throw the ArithmeticException. An underflow generates zero. An overflow leads to wrap-around, i.e., adding 1 to the maximum integer wraps around to the minimum integer.

An op= assignment operator corresponds to each of the binary operators in the above table.

The integer relational operators <, >, <=, >=, ==, and != produce boolean results.

#### 7.1.2 Operators on Boolean Values

Variables or expressions that are boolean can be combined to yield other boolean values. The unary operator ! is boolean negation. The binary operators &, |, and ^ are the logical AND, OR, and XOR operators; they force evaluation of both operands. To avoid evaluation of right-hand operands, you can use the short-cut evaluation operators && and ||. You can also use == and !=. The assignment operators also work: &=, |=, ^=. The ternary conditional operator ?: works as it does in C.

#### 7.1.3 Operators on Floating Point Values

Floating point values can be combined using the usual operators: unary -; binary +, -, *, and /; and the assignment operators +=, -=, *=, and /=. The ++ and -- operators also work on floating point values (they add or subtract 1.0). In addition, % and %= work on floating point values, i.e.,

        a % b

is the same as:

        a - ((int)(a / b) * b)

This means that a%b is the floating point equivalent of the remainder after division.

Floating point expressions involving only single-precision operands are evaluated using single-precision operations and produce single-precision results. Floating point expressions that involve at least one double-precision operand are evaluated using double-precision operations and produce double-precision results.

The language has no arithmetic exceptions for floating point arithmetic. Following the IEEE 754 floating point specification, the distinguished values Inf and NaN are used instead. Overflow generates Inf. Underflow generates 0. Divide by zero generates Inf.

The usual relational operators are also available and produce boolean results: >, <, >=, <=, ==, !=. Because of the properties of NaN, floating point values are not fully ordered, so care must be taken in comparison. For instance, if a<b is not true, it does not follow that a>=b. Likewise, a!=b does not imply that a>b || a<b. In fact, there may no ordering at all.

Floating point arithmetic and data formats are defined by IEEE 754, "Standard for Floating Point Arithmetic." See Appendix: Floating Point for details on the language's floating point implementation.

#### 7.1.4 Operators on Arrays

The following:

        <expression>[<expression>]

gets the value of an element of an array. Legal ranges for the expression are from 0 to the length of the array minus 1. The range is checked only at runtime.

#### 7.1.5 Operators on Strings

Strings are implemented as String objects (see String Literals for more information). The operator + concatenates Strings, automatically converting operands into Strings if necessary. If the operand is an object it can define a method call toString() that returns a String in the class of the object.

        // Examples of the + operator used with strings
        float a = 1.0;
        print("The value of a is " + a + "\n");
        String s = "a = " + a;

The += operator works on Strings. Note, that the left hand side (s1 in the following example) is evaluated only once.

        s1 += a; //s1 = s1 + a; // a is converted to String if necessary

#### 7.1.6 Operators on Objects

The binary operator instanceof tests whether the specified object is an instance of the specified class or one of its subclasses. For example:

        if (thermostat instanceof MeasuringDevice) {
            MeasuringDevice dev = (MeasuringDevice)thermostat;
              ...
        }

determines whether thermostat is a MeasuringDevice object (an instance of MeasuringDevice or one of its subclasses).

### 7.2 Casts and Conversions

The Java language and runtime system restrict casts and conversions to help prevent the possibility of corrupting the system. Integers and floating point numbers can be cast back and forth, but integers cannot be cast to arrays or objects. Objects cannot be cast to base types. An instance can be cast to a superclass with no penalty, but casting to a subclass generates a runtime check. If the object being cast to a subclass is not an instance of the subclass (or one of its subclasses), the runtime system throws a ClassCastException.

Next Prev Up Contents

The Java Language Specification

Generated with CERN WebMaker