next up previous
Next: 1.2.3 Precedence of Operations Up: 1.2 Java Mechanics Previous: 1.2.1 Notation and Syntax

1.2.2 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)
Like the C programming language, Java uses the symbol && for the ``and'' operation on boolean values (true and false) and the symbol == for the equality operation on numbers. (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  
star 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 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 result
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 expression
produces the result
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

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

5 % 3
5 / 0
5 < 6
5. < 6.
3 + .100000000 * .10000000 - 3.
Java expressions directly in the Interactions window. Did you get the answers that you expected?

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

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 1.2.3.

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

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 window, 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 up previous
Next: 1.2.3 Precedence of Operations Up: 1.2 Java Mechanics Previous: 1.2.1 Notation and Syntax
Corky Cartwright