next up previous
Next: 1.3.2 Java Statements Up: 1.3 Java Notation and Previous: 1.3 Java Notation and

1.3.1 Java Expressions

In Java, arithmetic and boolean expressions are written in conventional mathematical infix notation, adapted to the standard computer character set (called ASCII). For example, the first two Scheme expressions from above would be written in Java as
    x*x + y*y
    (x > 0) && (y == z)
Like C, Java uses the symbol && for the boolean ``and'' operation and the symbol == for the equality operation on numbers. (The symbols & and = are used in the C and Java for other purposes.)

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

+ addition  
- 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  

Java also supports the prefix operators - (negation) and ! (not) used in conventional mathematical notation.

Finger Exercise: Conduct some experiments to see how these operators behave by writing simple expression evaluation programs in Java (as shown in the previous section).

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 and the value of alternative otherwise.


next up previous
Next: 1.3.2 Java Statements Up: 1.3 Java Notation and Previous: 1.3 Java Notation and
Robert Cartwright, Spring 1999