Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - Relational Operators



The following programs are simple examples which demonstrate the relational operators. Copy and paste the following Java programs as Test.java file, and compile and run the programs −

Example 1

In this example, we're creating two variables a and b and using relational operators. We've performed equality and non-equality checks and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;

      System.out.println("a == b = " + (a == b) );
      System.out.println("a != b = " + (a != b) );
   }
}

Output

a == b = false
a != b = true

Example 2

In this example, we're creating two variables a and b and using relational operators. We've performed greater than and less than checks and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;

      System.out.println("a > b = " + (a > b) );
      System.out.println("a < b = " + (a < b) );
   }
}

Output

a > b = false
a < b = true

Example 3

In this example, we're creating two variables a and b and using relational operators. We've performed greater than or equal to and less than or equal to checks and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;

      System.out.println("b >= a = " + (b >= a) );
      System.out.println("b <= a = " + (b <= a) );
   }
}

Output

b >= a = true
b <= a = false
java_basic_operators.htm
Advertisements