Home > Java > Operators > Relational Operators > Introduction
Introduction

Introduction to Relational Operators

Relational operators generate a boolean result. They evaluate the relationship between the values of the operands. A relational expression produces true if the relationship is true, and false if the relationship is untrue. The relational operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent (!=).

 

Syntax


operand1 < operand2   // Result is true if operand1 is less than operand2 else false.
operand1 > operand2 // Result is true if operand1 is greater than operand2 else false.
operand >= operand2  // Result is true if operand1 is greater than or equal to operand2 else false.
operand1 <= operand2  // Result is true if operand1 is less than or equal to operand2 else false.
operand1 == operand2 // Result is true if operand1 and operand2 are equal else false.
operand != operand2  // Result is true if operand1 is not equal to operand2 else false.

 

Example


1. /**
2. * @(#)RelationalOperatorsDemo.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/3
7. */
8.
9.   public class RelationalOperatorsDemo {
10.        
11.    /**
12.    * Creates a new instance of RelationalOperatorsDemo.
13.     */
14.    public RelationalOperatorsDemo() {
15.    }
16.    
17.    /**
18.     * @param args the command line arguments
19.     */
20.    public static void main(String[] args) {
21.        // TODO code application logic here
22.        
23.     int a = 25;
24.     int b = 10;
25.        
26.     System.out.println("Value of a is greater than value of b: " + (a > b) );
27.     System.out.println("Value of a is less than value of b: " + (a < b) );
28.     System.out.println("Value of a is greater than or equal to value of b: " + (a >= b) );
29.     System.out.println("Value of a is less than or equal to value of b: " + (a <= b) );
30.     System.out.println("Value of a is equal to value of b: " + (a == b) );
31.     System.out.println("Value of a is not equal to value of b: " + (a != b) );
32.   }
33. }


In above example, expression is enclosed with ( ) so that it evaluates before concatenation. The ‘+’ is used to concatenate strings.


Output


Value of a is greater than value of b (a > b): true
Value of a is less than value of b (a < b): false
Value of a is greater than or equal to value of b (a >= b): true
Value of a is equalless than or equal to value of b (a <= b): false
Value of a is equal to value of b (a == b): false
Value of a is not equal to value of b (a != b): true

Leave a Reply

Fields with * are mandatory.
* Name:
Website:
* Comments:
 
Tags allowed: <strong> <i> <u>
* Code:
 
 
Submit   Cancel