Читайте также: |
|
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:
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"); // 2else System.out.println("no"); int c = 4;if (a < b + 1 == b < c) System.out.println("yes"); // 3else System.out.println("yes");no
no
yes
The program prints the following output to the command prompt:
Notes:
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.
Дата добавления: 2015-11-16; просмотров: 87 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
A brief survey of control statements. | | | Logical operators and expressions |