1.2.2 Java Expressions

is written in Java as(and (< (+ (* x x) (* y y)) 25) (> x 0))

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

`&&`

for
the ``and'' operation on boolean values (`&`

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

+addition and Stringconcatenation-subtraction starmultiplication /division %mod (remainder from integer division) <less than <=less than or equal >greater than >=greater than or equal ==equal !=not equal &&and ||or

The arithmetic operators all use conventional computer arithmetic appropriate for the types of the arguments. Conventional computer arithmetic does not exactly conform to the standard mathematical conventions. For example, the expression

produces the result5/3

which is the quotient of 5 divided by 3. Integer division produces an integer rather than rational result; it truncates to the decimal representation of the rational result. Similarly, The expression1

produces the result5%3

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

is equivalent to the expression5 / 3

5/3

**Finger Exercise:** In the DrJava programming environment, try
evaluating the following expressions:

Java expressions directly in the5/3 5 % 3 5./3. 5 / 0 5./0. 5 < 6 5. < 6. 3 + .100000000 * .10000000 - 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 coerces the other argument to a `String`.
Object values are coerced to type `String` using their `toString()` methods. As we explain in Section 1.4.4, *every* object has a `toString()` method. Primitive values are
converted to strings using built-in conversion routines such as the
static method `Integer.toString(int i)` which converts an `int` to the corresponding `String`.

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

evaluates to the9 + 5 + 1 + "S"

evaluates to the9 + (5 + (1 + "S"))

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.

**Finger Exercise:** If the DrJava *Interactions* window, try
evaluating the following expressions:

-5 + 3 ! (5 < 6)

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

borrowed fromtest?consequent:alternative

Note that when(cond (testconsequent) (elsealternative))

does not divide 2 by 0. The(2 < 0) ? 2/(1 - 1) : 0

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

The last example produces a syntax error because(2 < 0) ? 2/(1 - 1) : 0 (0 < 1) ? "foo" : "bar" 17 ? true : false