4.1.1 Java Expressions
Keywords: current

Next: 4.2 Precedence of Operations Up: 4.1 Notation and Syntax Previous: 4.1 Notation and Syntax

##
4.1.1 Java Expressions

In Java, arithmetic, boolean, and String expressions are written in conventional mathematical infix notation, adapted to the standard computer character set (called ASCII). For example, the Scheme expression

    (and (< (+ (* x x) (* y y)) 25) (> x 0))

is written in Java as

    (x*x + y*y > 25) && (x > 0)

The syntax of Java expressions is patterned after the C programming language. Like C, Java uses the symbol `&&` for the ``and'' operation on boolean values (`true` and `false`) and the symbol `==` for the equality operation on numbers. (Warning: the symbols `&` and `=` are used in C and Java for other purposes.)

The following table lists the major infix operators provided by Java:

> `+` | addition and `String` concatenation |
> `-` | subtraction |
> `*` | multiplication |
> `/` | division |
> `%` | mod (remainder from integer division) |
> `<` | less than |
> `<=` | less than or equal |
> `>` | greater than |
> `>=` | greater than or equal |
> `==` | equal |
> `!=` | not equal |
> `&&` | and |
> `||` | or |

The Java arithmetic operators all perform the indicated operations using computer arithmetic instead of genuine arithmetic. Computer arithmetic does not exactly conform to the standard mathematical conventions. Calculations involving real numbers (Java types `float` and `double`) are approximate; the computer rounds the true result to the nearest real number expressible using the number of digits provided in the standard machine representation (scientific notation with a fixed number of digits for the fraction and exponent). Integer calculations are done exactly provided that the answer is an integer and that it can be represented using 31 binary digits plus a sign.4.1 Note that integer division always produces integer answers (unless you try to divide by zero which is an error). For example, the expression

> `5/3`

produces the result

> `1`

which is the quotient of 5 divided by 3. Integer division truncates the true rational result, dropping the digits to the right of the decimal point. Similarly, The expression

> `5%3`

produces the result

> `2`

which is the remainder of 5 divided by 3. In Java program text, spaces between symbols are ignored; the expression

> `5 / 3`

is equivalent to the expression

> `5/3`

All of the binary infix operators in Java are either arithmetic, relational, or boolean except for `+` when it is used in conjunction with strings. If either argument to `+` is of `String` type, then Java converts the other argument to a `String`. Object values are coerced to type `String` using their `toString()` methods. As we explain in Section 1.3.4, every object has a `toString()` method. The concatenation operator converts primitive values to strings using built-in conversion routines that we will discuss later.

Note that the order in which arguments appear and the use of parentheses in mixed integer and string expressions constructed from `int` and `String` values affects the conversion process. For example, the expression

> `9 + 5 + 1 + "S"`

evaluates to the `String` `"15S"` while the expression

> `9 + (5 + (1 + "S"))`

evaluates to the `String` `"951S"`. The association rules for Java expressions are explained in Section 4.2.

Java also supports the unary prefix operators `-` (arithmetic negation) and `!` (boolean ``not'') used in conventional mathematical notation. Parentheses are used to indicate how expressions should be decomposed into subexpressions. All of the binary infix operators in Java are either arithmetic, relational, or boolean except for `+` when it is used in conjunction with strings. If either argument to `+` is of `String` type, then Java converts the other argument to a `String`. Object values are coerced to type `String` using their `toString()` methods. As we explain in Section 1.3.4, every object has a `toString()` method. The concatenation operator converts primitive values to strings using built-in conversion routines that we will discuss later.

Note that the order in which arguments appear and the use of parentheses in mixed integer and string expressions constructed from `int` and `String` values affects the conversion process. For example, the expression

> `9 + 5 + 1 + "S"`

evaluates to the `String` `"15S"` while the expression

> `9 + (5 + (1 + "S"))`

evaluates to the `String` `"951S"`. The association rules for Java expressions are explained in Section 4.2.

Java also supports the unary prefix operators `-` (arithmetic negation) and `!` (boolean ``not'') used in conventional mathematical notation. Parentheses are used to indicate how expressions should be decomposed into subexpressions.

The only pure expression form in Java that deviates from conventional mathematical notation is the conditional expression notation

> test `?` consequent `:` alternative

borrowed from C. This expression returns the value of consequent if test is `true` and the value of alternative if test is `false`. It corresponds to the Scheme expression

> `(cond [test consequent] [else alternative])`

Note that when test is `true`, alternative is not evaluated. Similarly, when test is `false`, consequent is not evaluated. Hence, the expression

> `(2 < 0) ? 2/(1 - 1) : 0`

does not divide 2 by 0. The test expression must be a boolean value, `true` or `false`.

Finger Exercise: In the DrJava Interactions pane, try evaluating the following expressions:

    (2 < 0) ? 2/(1 - 1) : 0
    (0 < 1) ? "foo" : "bar"
    17 ? true : false

The last example produces a syntax error because `17` is not a boolean value.

Next: 4.2 Precedence of Operations Up: 4.1 Notation and Syntax Previous: 4.1 Notation and Syntax