Java: Relational Operators

Relational operator in Java

Operators

Java provides quite a variety of operators which come in handy to the programmer for the manipulation of variables. One such is Relational operator in Java which we are going to disucss on this page.

Relational Operators

Relational operators are used in making decisions and in loops. They are used to test or define some relation between entities.

They give output as a boolean function which helps decide the control flow to decide which way to go.

OperatorFunctionalitySyntax
== (equal to)To check if two operands are equal or not, if yes, the output becomes true.a==b
!= (not equal to)To check if two operands are equal or not, if not, the output becomes true.a!=b
> (greater than)To check if the left operand is greater than the right operand, if yes, then the output becomes true.a>b
< (less than)To check if the left operand is less than the right operand, if yes, the output becomes true.a<b
>= (greater than or equal to)To check if the left operand is greater than or equal to the right operand, if yes, then the output becomes true.a>=b
<= (less than or equal to)To check if the left operand is less than or equal to the right operand, if yes, the output becomes true.a<=b

Syntax with Example

Run

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

  // create variables
  int a = 10, b = 6;

  // value of a and b
  System.out.println("a is " + a + " and b is " + b);

  // == operator
  if(a == b)
    System.out.println("(a == b) :true");
  else
    System.out.println("(a == b) :false");

  // != operator
  if(a != b)
    System.out.println("(a != b) :true");
  else
    System.out.println("(a != b) :false");

// > operator
   if(a > b)
    System.out.println("(a > b) :true");
  else
    System.out.println("(a > b) :false");

  // < operator
  if(a < b)
    System.out.println("(a < b) :true");
  else
    System.out.println("(a < b) :false"); // >= operator
  if(a >= b)
    System.out.println("(a >= b) :true");
  else
    System.out.println("(a >= b) :false");

  // <= operator
  if(a <= b)
    System.out.println("(a <= b) :true");
  else
    System.out.println("(a <= b) :false");
 }
}

Output

a is 10 and b is 6
(a == b) :false
(a != b) :true
(a > b) :true
(a < b) :false
(a >= b) :true
(a <= b) :false

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription