Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

9.2. Comparison operators and expressions

Many control statements use conditions given as relational expressions built with the help of relational operators or equality operators. Relational operators (<, <=, >, >=) are two argument (binary) operators. Each argument is an expression of numeric type (byte, char, short, int, long, float, double). The value of a relational expression is of boolean type (true or false).

Relational operators

Expression (a and b - arbitrary expressions of numeric types)

Result

a > b

true if the value of a is grater then the value of b false in another case

a >= b

true if the value of a greater than or equal to the value of b false in another case

a < b

true if the value of a is less then the value of b false in another case

a <= b

true if the value of a less than or equal to the value of b false in other case

Examples:

int a =1, b = 3; if (a > b) System.out.println("Greater than"); else System.out.println("Less than");

Prints out: Less than

int a =1, b = 3; if (a + 1 > b/3) System.out.println("Greater than"); else System.out.println("Less than"); Note: the precedence of relational operators is lower than the arithmetical operators' precedence Thus we don't have to write: if ( (a + 1) > (b/3) ) ...

Prints out: Greater than

int a = 1, b = 3; boolean reslt1 = a <= b; boolean reslt2 = 1 >= a; boolean reslt3 = a < 1; Note: the precedence of relational operators is higher than the assignment operator's precedence Thus we don't have to write: boolean reslt = (a <= b);

The variables have the following values: reslt1 == true reslt2 == true reslt3 == false

A common error is using the assignment operator (=) in place of the equality operator (==). Usually the compiler detects this error and prints appropriate warning.

The two binary equality operators: == and != do not belong to the group of relational operators for two reasons:

  • they have lower precedence than relational operators

  • their arguments may be of any type (numeric, boolean, reference) under the following conditions:

    • if one of the arguments is of numeric type - the other must be of numeric type too

    • if one of the arguments is of boolean type - the other must be of boolean type too

    • if one of the arguments is of reference type then the other must be of reference type compatible with the type of the first argument or the null literal

Expressions built using equality operators have results of type boolean.

Equality operators

Expression

a == b

a != b

a and b of arbitrary numeric type

true if the value of a is equal to b false in other case

true if the value of a is not equal to b false in other case

a and b of type boolean

true if a and b are both true or false false in other case

true if a is true and b is false or a is false and b is true false if both a and b are true or false at the same time

a and b of reference type

true if both a and b refer to the same object or have the value of null false if a refers to other object than b or exactly one of the variables has the value of null

true if both variables refer to different objects or exactly one of them is null false if both variables refer to the same object or both have the value of null

Example:

int a = 2, b = a + 1;

if ( a == b) System.out.println("yes");

else System.out.println("no");

if ( a != --b) System.out.println("yes"); // 2

else System.out.println("no");

int c = 4;

if ( a < b + 1 == b < c) System.out.println("yes"); // 3

else System.out.println("yes");

no no yes

The program prints the following output to the command prompt: Notes:

  1. The prefix operator "--" assures that the value of b in the example 2 will be decreased before its use in the equality expression (!=)

  2. The example 3 uses the fact that the equality operator ("==") precedence is lower than relational operators precedence, which is lower than arithmetical operator precedence (+). Thus two values of boolean type are compared there and because they are true - the result is true too.

However, complex expressions should not be noted as in the example 3. It is better to use parentheses to make the code more clear. Example on comparing references: Pair p1 = new Pair(1,1); Pair p2 = new Pair(1,1); Pair p3 = p1; p1 == p2 // false although components of both pairs are the same p1 == p3 // true because both variables refer to the same object String s1 = "1,1"; p1 == s1 // compilation error: p1 is of type Pair and s1 is of type String which are not compatible String s2 = "1," + 1; s1 == s2 // false because variables refer to different objects s1 == null // false because s1 refer to existing object s1.equals(s2) // true because the strings are the same

To test the contents of different objects for equality the equals method is used. It must be defined in objects' class.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]