Relational and Conditional Operators in Java

In this tutorial we will see the details about relational and conditional operators in java. The tutorial covers details like what these operators are, how to use them, list of all relational/conditional operators and java programs of these operators.

Relational operators in Java

Relational operators are used to relate or compare if one operand is greater than, less than, equal to, or not equal to another operand. Operators like >, >=, < etc are called as relational operators while operator == is called as equality operator. All relational operators along with equality operator returns either true or false. Table below shows the list of all relational operators in java.

List of Relational operators in Java

Operator Name Example Description
== Equale to Operator a==b Checks if a and b are equal
!= Not equal to Operator a!=b Checks if a and b are not equal
> Greater than Operator a>b Checks if a greater than b
>= Greater than or equal to Operator a>=b Checks if a greater than or equal to b
< Less than Operator a < b Checks if a less than b
<= Less than or equal to Operator a<=b Checks if a less than or equal to b

All relational operators are binary operators which means they operates on two operands. The operands in a relational operator can be a constant value, a variable, an expression or a function call returning a compatible value.

 int a = 10, b = 20; boolean b2;
 b2 = 5 > 6;  // operand can be a constant value, b2 = false
 b2 = a < b; // operand can be variables, b2 = true
 b2 = a <= (b-10); // operand can be an expression, b2 = true
 b2 = a >= sum(5,10); // operand can be a method(sum) call, b2 = false

Relational operators are mostly used with conditional and looping statements like if, if else, for, while etc.

 int a = 10, b = 20; boolean b2;
 if(a > b)
 while(a > b)

There can be spaces before and after all relational operator. Also remember that, in multi-character relational operators like >=, <= ==, there should not be any space between the characters of the operator. For example = =, > =, < = would result in compilation error.

 int a = 10, b = 20;
 if(a>b) or if(a  >  b)  // Correct, spaces are allowed before and after the operator
 if(a>=b)  or if(a  >=  b)  // Correct use of multi-character operator
 if(a  > = b)  // Incorrect use, spaces not allowed within multi-character operators

All relational operators can be used with primitive data types only except equality(==) operator which can be used with non primitive data types as well. The equality operators can be used to compare if two objects have same references(address).

 String str = new String("refresh java");
 String str2 = new String("Refresh Java");
 String str3 = str;
 if(str == str2)  // condition will result as false
 if(str == str3)  // condition will result as true
 if(str > str2)  // Incorrect, operator > can't be use with non primitive types

The result of a relational operator is always a boolean value that is true or false. So they must be assigned into boolean type variable only, assigning in any other data type variable will result in compilation error.

Java program of relational operator

 class RelationalOperator {
   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 <= num2)
        System.out.println("num1 <= num2");
    }
 }	

Output:

num1 != num2
num1 < num2
num1 <= num2

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

Operator = is an assignment operator which is used to assign value in a variable while == is an equality operator which is used to compare if two variables/values are equal or not.

Conditional operators in Java

Following operators are conditional operators in java :

Operator Name Example Description
&& Conditional AND Operator (a==1 && b==5) Conditional-AND operator, returns true if both expression returns true, else false
|| Conditional OR Operator (a==1 || b==5) Conditional-OR operator, returns true if any of the expression returns true, else false

The result of conditional AND and conditional OR operators is also a boolean value which is true or false.

What is Logical AND and Logical OR operators in java ?

The conditional-AND (&&) and conditional-OR (||) operators in java is also known as Logical AND(&&) and Logical OR(||) operators respectively.

Both these operators are also binary operators, since they operate on two operands/expressions. The left and right expressions of these operators must be a boolean variable/value, expression or a function returning a boolean value. The operands can not be a constant value like 5, 10 etc.

 int a = 10, b = 20, boolean b2 = true;
 if(b2 && a > 5) // operand can be a boolean variable or expression
 if(a > 5 || b > 20) // operands can be an expression
 if(a > 5 && isValid()) // operands can be an expression or function returning a boolean value
 if(5 && 10) // Incorrect, operands can not be a constant value.

The block below shows what would be the result of && and || operators when it's left and right expressions/conditions returns true or false.

 true && true  => true;            true || true  => true;    
 true && false => false;           true || false => true;
 false && true => false;           false || true => true;
 false && false => false;          false || false => 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.

 int a = 10, b = 20;
 if(a > 12 && b > 15) // Here b > 15 won't be checked as a > 12 returns false
 if(a > 5 || b > 20) // Here b > 20 won't be checked as a > 5 returns true

There should not be any space between the characters of conditional AND and OR operators. For example & & or | | will result in compilation error.

 int a = 10, b = 20;
 if(a > 12 & & b > 15) // Incorrect, there should not be any space in &&
 if(a > 5 | | b > 20) // Incorrect, there should not be any space in ||

These operators can also be repeated any number of times inside an expression and they can be mixed as well.

 int a = 10, b = 20;
 if(a > 5 && a < 20 && b > 15) // returns true, as all conditions are true
 if((a > 10 && a < 20) || b > 15) // returns true, as b > 15 returns true

Java Ternary Operator

The ternary operator( ?:) is another conditional operator in java. This operator can be thought as a short form of if-then-else statement. It is known as ternary operator because it uses three operands. Line below shows how to use this operator.


 variable = someCondition ? Expression1 : Expression2; 

Here someCondition is a boolean expression that returns either true or false. You can read this operator as "if someCondition is true, assign the value of Expression1 into the variable else assign the value of Expression2 into the variable.

What is boolean expression in java ?

An expression which returns either true or false is known as boolean expression. For example a > b, a == b, (a + b) < c etc are boolean expressions.

Here Expression1 and Expression2 can be a constant value, a variable or an expression like a+b, a*b+c, a>b, "hello" etc. The value of these expressions must match with data type of the variable.

 int a = 10, b = 20;
 int result = a > b ? 5 : 1; // Expression can be a constant value
 int result2 = a > b ? a : b; // Expression can be a variable
 int result3 = a > b ? (a-b) : (b-a);
 String str = (a == b) ? "Equal" : "Not Equal";

Java program of conditional operator

 class ConditionalOperator {
   public static void main(String[] args) {
     int num1 = 3, num2 = 5, num3 = 10;
        
     if(num1 == 3 && num2 == 5)
       System.out.println("num1 = 3 AND num2 = 5");
     if(num1 > 0 && num2 > 1 && num3 >= 10)
       System.out.println("num1 > 0 AND num2 > 1 and num3 >= 10");
     if(num1 == 3 || num2 == 5)
       System.out.println("num1 is 3 OR num2 is 5");
     if((num1 > 0 || num2 > 1) && num3 >10)
       System.out.println("Print me if condition is true");  
        
     int result = num1 < num2 ? num1 : num2;
     String str = num1 == num2 ? "num1 and num2 is equal" : "num1 and num2 is not equal";
     System.out.println("result = "+result);
     System.out.println("str = "+str);
   }
 }	

Output:

num1 = 3 AND num2 = 5
num1 > 0 AND num2 > 1 and num3 >= 10
num1 is 3 OR num2 is 5
result = 3
str = num1 and num2 is not equal

★★★
  • 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.