|
|
The example includes a user defined constant (we have not had an example programme yet which includes user defined constants, although we have used the Math class constant PI). The application class in the example comprises two static methods (main and one other) --- so far all the application classes we have looked at have included only one (main) method. In addition the example, as with the Point_2D class studied earlier, includes the passing of objects as arguments (formal parameters) to methods.
1. EXPRESSIONS |
|
|
2. BOOLEAN EXPRESSIONS |
Expressions that use Boolean operators (named after the 19th century mathematician George Boole) return a data item of type boolean which can take the values true or false. Well know Boolean operators are given in the Table 1 to the right. The first 6 are sometimes referred to as comparison or relational operators and the remaining 4 as logical operators. Examples of Boolean expressions: operand1 < 50 operand2 == 0 operand3 > = 10 & operand3 < = 20 operand3 < 10 | operand3 > 20 operand1 < 20.0 & (operand2 != 'q' | myInstance.testFun(operand1, operand4) == 1)
Table 1. Boolean operations These expressions can be interpreted as follows:
|
The comparison operators may be applied to both numeric types and characters. The result of comparison on characters will depend on the unicodes associated with the characters in question. For example 'B' < 'a' is true ('B' has a unicode of 66 while 'a' has a unicode of 97). ![]() Figure 2: George Boole (1) |
3. OPERATORS |
![]() | The number of operands that an operator requires is referred to as its arity. |
![]() | Operators with one operand are called monadic or unary. |
![]() | Operators with two operands are called dyadic or binary. |
![]() | Some programming languages (but not Java) support ternary operators. |
![]() | Operators can also be classified as prefix, infix or postfix. |
Table 2 shows the precedence levels/ordering of a number of common operators starting with low level operators and progressing down through higher level operators. The order of execution of operators on the same level is controlled by associativity:
If an operator is non-associative it cannot be used in a sequence. For example the comparison operator (==) is non-associative. Thus we cannot write a boolean expression of the form: sideA == sideB == sideC |
Table 2. Precedence ordering of operations instead we must write ((sideA == sideB) & (sideB == sideC)) Note that the & operator is associative (as are the &&, |, || and ^ operators). Java also provides at least one triadic operator (an operator that has three operands): operand1 ? opeerand2 : operand3 |
4. EVALUATION OF EXPRESSION |
Consider the Java code presented in Table 3. (Ignoring the fact that we should not be using single letter identifiers) we can evaluate the expression a+b*c-d/e according to the Java precedence and association rules for the individual operators. Replacing the variables with their values we get: 1+2*3-4/5 The expression contains two level 3 operators (* and /), and two level 4 operators (+ and -). Level 3 operators are evaluated before level 4 operators, and all Java arithmetic operators are left associative. Thus the first sub expression evaluated is b*c, i.e. 2*3 because this is the first operator (reading from left to right) with the lowest precedence. This will give 6. We now have: 1+6-4/5 |
Next we evaluate 4/5 (d/e) which will give 0 (remember we are doing integer division). The expression now becomes: 1+6-0 We now evaluate the 1+6 part to give 7 and: 7-0 will give 7. When working with expressions of the form shown in Table 3 it is a good idea to include parenthesis so as to make the expression as clear as possible. Thus: a + (b*c) - (d/e) Note also that the division operator ('/') when used with operands of type int produces "integer division"; this is why in the above example 4/5 gives the result 0. |
// OPERATOR EVALUATION // Frans Coenen // Monday 12 March 1999 // The University of Liverpool, UK class OperatorEvaluation{ // --------------- METHODS ------------------ /* MAIN METHOD: */ public static void main(String[] args) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int answer; answer = a+b*c-d/e; System.out.println("Answer = " + answer); } } |
Table 3: Operator evaluation program
5. TRUTH TABLES |
The operation of the logical operators can be illustrated using what are known as truth tables. Four such tables are given in Figure 1, one for each of the logical operators given above. If we consider the table for the logical "and", this is an infix binary operator and this has two operands (as do the logical "or" and "exclusive or", but not the "not" which is prefix unary operator). The inputs can be true or false (if the inputs are literals then they are automatically true). Thus for the binary operators we have four possible combinations of input which are given in the left two columns of the Tables in Figure 1. |
For a logical "and" to return true, both inputs must be true --- as evidenced by the table. The distinction between a logical "or" and an "exclusive or" is that in the first case one or other or both operands must be true, while in the second case one or other but not both operands must be true. The above has implications for testing. Given a logical "and", "or" or "exclusive or" we should derive test cases for all four possible combinations. Similarly, given a "logical not" we should test for both combinations. |
|
|
|||||||||||||||||||||||||||||||
|
|
Figure 1: Truth tables
6. LAZY EVALUATION (SHORT CIRCUIT EVALUATION) |
Consider the expression: operand1 & operand2 If operand1 is false then the expression will return false regardless of the value of operand2. Similarly in the case of: operand1 | operand2 If operand1 is true then the expression will return true regardless of the value of operand2. |
In both cases the second operand has been evaluated needlessly. Java therefore provides "short cut" versions of & and |, namely && and || which avoid this needless evaluation. Thus: operand1 && operand2 operand1 || operand2 The mechanism where by we only evaluate operands if we need to is called lazy evaluation or short circuit evaluation. Some example code illustarting the distinction between the & and && is available. |
7. STATEMENTS |
Statements are the commands in a language that perform actions and change the "state" (the term is used for historical reasons). The state of a program is described by the values held by its variables at a specific point during its execution. We can identify six broad categories of statement common to most OOP languages:
|
A Similar categorisation of statements is found in imperative programming languages. Statements are usually separated by some operator (symbol), in Java this is the semi-colon character (;). |
We have met the concept of assignment previously. The value of a variable can always be modified using an assignment statement: NUMBER := 2; |
|
|
Where necessary, in Java, we can sometimes "force" type equivalence using a type conversions (cast). T(N) |
Created and maintained by Frans Coenen. Last updated 10 February 2015