The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Relational and Conditional Operators

A relational operator compares two values and determines the relationship between them. For example, != returns true if the two operands are unequal. This table summarizes Java's relational operators:

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 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

Following is an example, RelationalDemo(in a .java source file), that defines three integer numbers and uses the relational operators to compare them. The relational operations are shown in red:

public class RelationalDemo {
    public static void main(String[] args) {

        //a few numbers
        int i = 37;
        int j = 42;
        int k = 42;

	//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

    }
}
Here's the output from this program, which is self-explanatory:
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
Relational operators often are used with the conditional operators to construct more complex decision-making expressions. One such operator is &&, which performs the boolean and operation. For example, you can use two different relational operators along with && to determine if both relationships are true. The following line of code uses this technique to determine if an array index is between two boundaries. It determines if the index is both greater than or equal to 0 and less than NUM_ENTRIES (which is a previously defined constant value):
0 ≤ index && index < NUM_ENTRIES
Note that in some instances, the second operand to a conditional operator may not be evaluated. Consider this code segment:
(numChars < LIMIT) && (...)
The && operator will return true only if both operands are true. So, if numChars is greater than LIMIT, the left-hand operand for && is false and the return value of && can be determined without evaluating the right-hand operand. In such a case, Java will not evaluate the right-hand operand. This has important implications if the right-hand 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 returns true if both are true. Likewise, | is similar to || (when its operands are boolean). This operator always evalutes both of its operands and returns false if they are both false. When their operands are numbers, & and | perform bitwise manipulations. The next section(in the Learning the Java Language trail) has more information.

Java supports five binary and one unary conditional operators, shown in the following table:

Operator Use Returns true if
&& op1 && op2 op1 and op2 are both true, conditionally evaluates op2
|| op1 || op2 either op1 or op2 is true, conditionally evaluates op2
! ! op op is false
& op1 & op2 op1 and op2 are both true, always evaluates op1 and op2
| op1 | op2 either op1 or op2 is true, always evaluates op1 and op2
^ op1 ^ op2 if op1 and op2 are different--that is if one or the other of the operands is true but not both


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form