- Home
- Objectives

- XyzWs Study Guides
- Study Guides
- Study Notes
- Resources

- Mock Exams
SCJP Study Guide:
Fundamentals
Printer-friendly version |
Mail this to a friend
Relational and Comparison Operators
Relational operators compare two values and determine the relationship between them. For example, != returns true if the two operands are unequal. The character-counting program uses != to determine whether the value returned by System.in.read() is not equal to -1.
Often the relational operators are used with another set of operators, the conditional operators, to construct more complex decision making expressions. One such operator is && which performs the boolean and operation.
| Operator | Use | Returns true if |
|---|---|---|
| > | op1 > op2 | op1 is greater than op2 |
| >= | op1 >= op2 | op1 is greater than or equal to op2 |
| < | op1 < op2 | op1 is less than to op2 |
| <= | op1 <= op2 | op1 is less than or equal to op2 |
| == | op1 == op2 | op1 and op2 are equal |
| != | op1 != op2 | op1 and op2 are not equal |
| instanceof | op1 instanceof op2 | op1 is not null and the reference could be cast to the op2 |
Integer Comparison Operators (<, <=, >, >=, ==, !=)
Each of these operators compares the numeric values of the two integer operands and returns a boolean value that indicates whether the particular relation is true or false.
Floating-Point Comparison Operators (<, <=, >, >=, ==, !=)
The operators compare the operands according to the rules of the IEEE 754 standard:
- If either operand is NaN, the result is false for all operators except !=, for which the result is true. For any two operands, x != y always produces the same result as !(x == y). However, when one or both operands are NaN, the <, >, <=, and >= operators do not produce the same results as the logical negation of the opposite operator. For example, if either of x and y is NaN, then x < y is false, but !(x >= y) is true.
-
When neither operand is NaN, the operators compare the values of the two floating-point operands with respect to the ordering
-∞ < -max < ... < -min < -0.0 == +0.0 < +min < ... < +max < +∞
where min and max are the smallest and largest positive finite values that can be represented in the given floating-point format. Notable effects of this ordering are:- Negative and positive zeros are considered equal.
- A negative infinity is considered less than all other values, but equal to another negative infinity.
- A positive infinity is considered greater than all other values, but equal to another positive infinity.
Boolean Equality Operators (==, !=)
The result of == is true if both x and y are true or if both x and y are false. Otherwise, the result is false.
The result of != is false if both x and y are true or if both x and y are false. Otherwise, the result is true. When the operands are of type boolean or Boolean, the != operator produces the same result as the ^ operator.
Reference Type Equality Operators (==, !=)
If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.
A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion. The run-time values of the two operands would necessarily be unequal.
At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.
The result of != is false if the operand values are both null or both refer to the same object or array; otherwise, the result is true.
While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).
Type Comparison Operator instanceof
The type of a op1 operand of the instanceof operator must be a reference type or the null type; otherwise, a compile-time error occurs. The op2 mentioned after the instanceof operator must denote a reference type; otherwise, a compile-time error occurs. It is a compile-time error if the op2 mentioned after the instanceof operator does not denote a reifiable type.
At run time, the result of the instanceof operator is true if the value of the op1 is not null and the reference could be cast to the op2 without raising a ClassCastException. Otherwise the result is false.
For example:
String s = "Reference";
if (s instanceof String) {
System.out.println("true");
}
The output is:
true
The following code, on the other hand, will produce compile time error, because String and Math do not have inheritance relationship.
String s = "Reference";
if (s instanceof Math) { //compile time error: incompatable conditional operand String and Math
System.out.println("true");
}
If a cast of the op1 to the op2 would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.
This operator can also be used to test whether a reference refers to an array. Since arrays are themselves objects in Java, this is natural enough, but the test actually checks two things : First it will check if the object is an array and then it will check if the element type of that array is some subclass of the element type of the right hand operand.