Skip to main content

Relational operators

Java | Relational Operators: Relational operators are using for compare the data.

List of Relational Operators:
.
    Less Than: This operator checks if left value is less than right value or not. it is denoted by '<'.
                                     Ex: 1 < 2.
    Greater Than: This operator checks if left value is greater than right value or not. it is denoted by '>'.
                                     Ex: 1 > 2.
     Equal to: This operator is using to check if both values are equal or not. it is denoted by '=='.
                                    Ex: 1 == 2.
      Not Equal to: This operator is using to check if both values are not equal or not. it is denoted by '!='.
                                     Ex: 1 != 2.
      Less Than or Equal to: This operator checks if left value is less than or equal with right value or not. it is denoted by '<='.
                                    Ex: 1 <= 2. 
      Greater Than or Equal to: This operator checks if left value is greater than or equal with right value or not. it is denoted by '>='.
                                     Ex: 1 >= 2.

Here is the Code:

  1. import java.io.*;

  2. class Main {
  3. public static void main (String[] args) {
  4.     // Less than
  5.     System.out.println(1 < 2);
  6.     // Greater than
  7. System.out.println(1 > 2);
  8. // Equal to
  9. System.out.println(1 == 2);
  10. // Not Equal to
  11. System.out.println(1 != 2);
  12. // Less than or Equal to
  13. System.out.println(1 <= 2);
  14. // Greater than or Equal to
  15. System.out.println(1 >= 2);
  16. // String Equals 
  17. System.out.println("abc".equals("abc"));
  18. }
  19. }

Output:
true
false
false
true
true
false
true

Here is the Video: