Expressions in Java

Home Up Search Java 2 API C++ Resources

(Jump down to the section on Operator Precedence)

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).   Infix notation is the standard arithmetic notation where the operation appears between the values upon which is operating.   For example:

(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. (Note: Java supports several different sizes of integer representation; 31 binary digits plus sign is the default for integer constants.) 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


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

5/3
5 % 3
5./3.
5 / 0
5./0.
5 < 6
5. < 6.
3 + .1 * .1 - 3.

Java expressions directly in the Interactions pane. 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 converts the other argument to a String. Object values are coerced to type String using their toString() methods. As we explain in Printing Object Values, 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 the section below on Operator Precedence.

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 2: If the DrJava Interactions pane, try evaluating the following expressions:

-5 + 3
-(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 3: 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.

 

 

Precedence of Operations

Since Java uses conventional infix notation for expressions it relies on the notion of precedence to determine how expressions like

12 * 5 + 10

should be interpreted. The Java operations given in the preceding subsection are divided into the following precedence groups:

prefix operators - !
multiplicative * / %
additive + -
relational < > >= <=
equality == !=
logical and &&
logical or ||
conditional ? ... : ...

from highest to lowest. A higher precedence operator has greater ``binding power''. For example, the expression

72. - 32. * 1.8

is equivalent to

72. - (32. * 1.8)

because * has higher precedence than infix -.


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

72. - 32. * 1.8
(72. - 32.) * 1.8

All of infix operators listed above are left-associative: when infix operators of equal precedence are chained together, the leftmost operator has precedence. For example,

72. - 30. - 12.

is equivalent to

(72. - 30.) - 12.

Parentheses can be used to override the built-in precedence and associativity of operators. Hence,

(72. - 32.) * 1.8

equals 40*1.8. Similarly,

72. - (30. - 12.)

equals

72. - 18.

It is a good idea to use parentheses if you have any doubts about the precedence relationship between consecutive operators. The judicious use of parentheses makes complex expressions easier to read.


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

72. - 30. - 12.
72. - (30. - 12.)

(Written by Corky Cartwright.  Modified by Stephen Wong)