Instructors' Guide

Introduction Terminology Expressions Control Objects Inheritance Technology Summary


Java supports basic types (including int, char and float) and compound types similar to C++. It does not offer pointer or reference types. However, it offers Array and String types. No need to say that it also allows for user-definable classes.

Type expressions

slide: Java -- expressions (1)

It will be no surprise that the expressions also are similar to those of C++. However, Java does not allow for operator overloading. That means that the operators, as listed below, may only be used for the built-in types.

The operators defined for the built-in types do sometimes behave in an unexpected way. For example, whereas the + operator defined for String concatenates two strings (as expected), the comparison operator == does not compare the values of the two strings, but instead the (opaque) references. One must use s1.equals(s2) to compare the values of the strings s1 and s2.



slide: Java -- expressions (2)

Value expressions may be created using arithmetic and comparison operators (including == for equality and != for inequality).

As logical operators Java includes, as C++, conjunction (&&) and disjunction ( || ), as well as a number of bitwise logical operators. Also, we have an indexing operator which, unlike for C++, may be not defined for arbitrary types. Access to both static and dynamic methods involves the use of the dot operator.

The increment and decrement are defined only for the scalar types. Also, we have a conditional expression of the form b?e_1:e_2 testing the condition b to deliver e_1 when it evaluates to true and e_2 otherwise.

Assignments in Java, like in C++, are written as var = expression with a single = symbol. As remarked previously, this convention is known to cause mistakes by programmers raised with languages such as Pascal or Modula-2.

In addition, Java offers, like C++, modifying assignments, which may be used as, for example, in n += 1, which is identical in meaning to n = n + 1.