refresh java logo

Relational And Conditional Operators

Relational operators are used to compare if one operand is greater than, less than, equal to, or not equal to another operand. Operators like >, >=, <, <=, != are called as relational operators while operator == is called as equality operator. All relational operators along with equality operator returns either true or false.

The Conditional-AND && and Conditional-OR || operators are known as conditional operators. These operators perform operations on two boolean expressions which means expression on left and right side of conditional operators must return either true or false. Conditional operators also returns either true or false.

In conditional && and || operator the second expression is evaluated only if needed. For example if first expression in && operator returns false, the second expression will not be evaluated because no matter what is the result(true or false) of second expression, the && operator will return false. Similarly in case of || operator if first expression returns true, the second expression will not be evaluated.

One more conditional operator is ?: which can be thought as short form of if-then-else statement. This operator is also known as ternary operator because it uses three operands. Below line shows how to use this operator.


 int result = someCondition ? value1 : value2; 

Here someCondition is a boolean expression that returns either true or false. You can read this operator as "if someCondition is true, assign the value1 to result else assign the value2 to result". Table below shows different types of relational and conditional operators along with their use.

Operator Example Description
== a==b equal to operator, checks if a and b are equal
!= a!=b not equal to operator, checks if a and b are not equal
> a>b greater than operator, checks if a greater than b
>= a>=b greater than or equal to operator, checks if a greater than or equal to b
< a < b less than operator, checks if a less than b
<= a<=b less than or equal to operator, checks if a less than or equal to b
&& (a==1 && b==5) Conditional-AND operator, returns true if both expression returns true else false
|| (a==1 || b==5) Conditional-OR operator, returns false if both expression returns false else true
?: result = someCondition ? a : b Ternary operator, if someCondition is true assign a into result else b into result

What is the difference between = and == operators in java ?

Operator = is used to assign value that is why it is called assignment operator while == used to compare if two operands or values or equal or not, that is why it is called as equality operator.

Java program of relational and conditional operator

 class ConditionalOperator
  {
     public static void main(String[] args)
      {
        int num1 = 3;
        int num2 = 5;
        if(num1 == num2)
          System.out.println("num1 == num2");
        if(num1 != num2)
          System.out.println("num1 != num2");
        if(num1 > num2)
          System.out.println("num1 > num2");
        if(num1 < num2)
          System.out.println("num1 < num2");
        if(num1 <= num2)
          System.out.println("num1 <= num2");
        if((num1 == 3) && (num2 == 5))
          System.out.println("num1 is 3 AND num2 is 5");
        if((num1 == 3) || (num2 == 5))
          System.out.println("num1 is 3 OR num2 is 5");
        
        int result = num1 < num2 ? num1 : num2;
        System.out.println("result = "+result);
      }
  }	

Output:

num1 != num2
num1 < num2
num1 <= num2
num1 is 3 AND num2 is 5
num1 is 3 OR num2 is 5
result = 3

★★★
  • An expression is constructed using variable, operators and method invocations. For example a, a+b, a*b+c are expressions.
  • The conditional && and || operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
  • Condition operators are also known as logical operators.