next up previous
Next: The Type boolean Up: Conditionals Previous: Conditionals

Relational Expressions

Now we know that multiplying an int by an int gives another int, or adding two ints gives an int and so on. That's because these operations which are associated with the type int have a return value of int. That's right -- you can think of * or + or - or / as being a method which takes two parameters, and returns a value. (We've been looking at things the other way up until now, thinking of the methods of a class as being analagous to the operations on an int, but thinking of the operations as methods is equally instructive).

For example, let's say that the Java language used the method multiply() to multiply two integers.

Then the declaration might look something like this:


        public static int multiply(int a, int b){
                return a*b;
        }

This is in some sense what the '*' symbol represents: a method which takes 2 integers as arguments and returns an integer. There is a different method (but the same symbol) for arguments of type double and so on. (If you think about it, the methods for int and double have to be different - a variable of type int has a different encoding than a variable of type double).

At any rate, there are other 'methods' for numeric types, and these 'methods' return a value of type boolean. Namely these 'methods' or operators are

  1. < less than
  2. > greater than
  3. <= less than or equal
  4. >= greater than or equal
  5. == equal
  6. != not equal

You can think of these as 'methods' that take two expressions of type int, for example, and return a value of type boolean. (The same holds for expressions of type double. Recall that when expressions involve both a double and an int, the int is promoted to a double and then the expression is evaluated. The same is true here when a variable of type int is compared to a variable of type double using one of the relational operators above.)

For example:

  1. 5 > 2 evaluates to true
  2. 2.3 > 5 evaluates to false
  3. 2 >= 2.0 evaluates to true
  4. 5 == 2 evaluates to false
  5. 5 != 2 evaluates to true

Recall that the possible values of an expression of type value are true and false. The list above shows what the expressions on the left evaluate to: either true or false.

Of course, we don't have to limit ourselves to comparing values to other values; we can compare the values stored in variables in the obvious way i > j, or we can comare the results of complicated mathematical expressions such as 4*i +7j + i*j == i+ 2*j - 3.

Of course, it's not absolutely clear how to evaluate this mathematical expression. For example, how do we now which operation to do first? For the answer to this, we have to look at operator precedence. By ranking the operators, we can decide which operation should be performed first: the operation with the higher-ranked operator should be performed first. See page 102 and the chart on page 145 of Nino and Hosch for a discussion of this.


next up previous
Next: The Type boolean Up: Conditionals Previous: Conditionals
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09