Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
A relational operator compares two values and determines the relationship between them. For example,!=
returnstrue
if its two operands are unequal. The next table summarizes the relational operators.
Relational Operators Operator Use Description >
op1 > op2
Returns true
ifop1
is greater thanop2
>=
op1 >= op2
Returns true
ifop1
is greater than or equal toop2
<
op1 < op2
Returns true
ifop1
is less thanop2
<=
op1 <= op2
Returns true
ifop1
is less than or equal toop2
==
op1 == op2
Returns true
ifop1
andop2
are equal!=
op1 != op2
Returns true
ifop1
andop2
are not equalThe following code is an example,
RelationalDemo
, that defines three integer numbers and uses the relational operators to compare them. The relational operations are shown in boldface.Here's the output from this program.public class RelationalDemo { public static void main(String[] args) { //a few numbers int i = 37; int j = 42; int k = 42; System.out.println("Variable values..."); System.out.println(" i = " + i); System.out.println(" j = " + j); System.out.println(" k = " + k); //greater than System.out.println("Greater than..."); System.out.println(" i > j = " + (i > j)); //false System.out.println(" j > i = " + (j > i)); //true System.out.println(" k > j = " + (k > j)); //false //(they are equal) //greater than or equal to System.out.println("Greater than or equal to..."); System.out.println(" i >= j = " + (i >= j)); //false System.out.println(" j >= i = " + (j >= i)); //true System.out.println(" k >= j = " + (k >= j)); //true //less than System.out.println("Less than..."); System.out.println(" i < j = " + (i < j)); //true System.out.println(" j < i = " + (j < i)); //false System.out.println(" k < j = " + (k < j)); //false //less than or equal to System.out.println("Less than or equal to..."); System.out.println(" i <= j = " + (i <= j)); //true System.out.println(" j <= i = " + (j <= i)); //false System.out.println(" k <= j = " + (k <= j)); //true //equal to System.out.println("Equal to..."); System.out.println(" i == j = " + (i == j)); //false System.out.println(" k == j = " + (k == j)); //true //not equal to System.out.println("Not equal to..."); System.out.println(" i != j = " + (i != j)); //true System.out.println(" k != j = " + (k != j)); //false } }Relational operators often are used with conditional operators to construct more complex decision-making expressions. The Java programming language supports six conditional operators five binary and one unary as shown in the following table.Variable values... i = 37 j = 42 k = 42 Greater than... i > j = false j > i = true k > j = false Greater than or equal to... i >= j = false j >= i = true k >= j = true Less than... i < j = true j < i = false k < j = false Less than or equal to... i <= j = true j <= i = false k <= j = true Equal to... i == j = false k == j = true Not equal to... i != j = true k != j = false
Conditional Operators Operator Use Description &&
op1 && op2
Returns true
ifop1
andop2
are bothtrue
; conditionally evaluatesop2
||
op1 || op2
Returns true
if eitherop1
orop2
istrue
; conditionally evaluatesop2
!
!op
Returns true
ifop
isfalse
&
op1 & op2
Returns true
ifop1
andop2
are both boolean and bothtrue
; always evaluatesop1
andop2
; if both operands are numbers, performs bitwiseAND
operation|
op1 | op2
Returns true
if bothop1
andop2
are boolean and eitherop1
orop2
istrue
; always evaluatesop1
andop2
; if both operands are numbers, performs bitwise inclusiveOR
operation^
op1 ^ op2
Returns true
ifop1
andop2
are different that is, if one or the other of the operands, but not both, istrue
One such operator is
&&
, which performs the conditionalAND
operation. You can use two different relational operators along with&&
to determine whether both relationships aretrue
. The following line of code uses this technique to determine whether an array index is between two boundaries. It determines whether the index is both greater than or equal to 0 and less thanNUM_ENTRIES
, which is a previously defined constant value.Note that, in some instances, the second operand to a conditional operator may not be evaluated. Consider this code segment.0 <= index && index < NUM_ENTRIESThe(numChars < LIMIT) && (...)&&
operator returnstrue
only if both operands aretrue
. So, ifnumChars
is greater than or equal toLIMIT
, the left operand for&&
isfalse
, and the return value of&&
can be determined without evaluating the right operand. In such a case, the interpreter does not evaluate the right operand. This case has important implications if the right operand has side effects, such as reading from a stream, updating a value, or making a calculation.When both operands are boolean, the operator
&
performs the same operation as&&
. However,&
always evaluates both of its operands and returnstrue
if both aretrue
. Likewise, when the operands are boolean,|
performs the same operation as||
. The|
operator always evaluates both of its operands and returnstrue
if at least one of its operands istrue
. When their operands are numbers,&
and|
perform bitwise manipulations. The next section contains more information on this.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.