The Relational Operators are used to check the relationship between the two operands. Relational Operators identifies the relationship with an operand over another operand. For example Ram is taller than Bala. The relational operators compares an operand with another operand to find the relationship between them. The relational operator in java returns output as either true or false.
The relational operators are widely used in java conditional statements such as if statement, switch statements and ternary operators. The java conditional statements compares the values using the relational operator for decision making the path of the execution. There are six types of relational operators supported in java to compare between two variables. These operands can be a variable, object or constants.
Relational Operators
Java has six types of relational operators to compare operands. The relational operators are equals to, not equals to, greater than, greater than or equals to, lesser than, lesser than or equals to. The output of these six relational operators are either true or false.
Operator Name | Operator | Example |
Equals to | == | x == y |
Not equals to | != | x != y |
Greater than | > | x > y |
Greater than or Equals to | >= | x >= y |
Lesser than | < | x < y |
Lesser than or Equals to | <= | x <= y |
Equals To ( == )
Equal to the relational operator is used to compare two values that are equal or not. The equals to relational operator is used between the same data types or between objects.
The equals to relational operator is used in between two objects compares the location of the memory of the object. If two variables are assigned to the same object and if you compare these two variables, the equal to relational operator returns true. Two different objects containing the same value will return false if you compare them with equals to relational operator.
package com.yawintutor;
public class RelationalEqual {
public static void main(String[] args) {
int a = 6;
int b = 6;
int c = 3;
boolean d = false;
boolean e = false;
d = (a == b);
e = (a == c);
System.out.println("a value is " + a);
System.out.println("b value is " + b);
System.out.println("c value is " + c);
System.out.println("\n(a == b) is " + d);
System.out.println("(a == c) is " + e);
}
}
Output
a value is 6
b value is 6
c value is 3
(a == b) is true
(a == c) is false
Not Equals To ( != )
Not equals to relational operator is used to check that two values are not the same or not. The Not equals to relational operator will return the opposite value of equals to relational operator. If two operands are matches, the not equals to operator will return false. If two operands are not same, the Not equals to relational operator will return true.
package com.yawintutor;
public class RelationalNotEqual {
public static void main(String[] args) {
int a = 6;
int b = 6;
int c = 3;
boolean d = false;
boolean e = false;
d = (a != b);
e = (a != c);
System.out.println("a value is " + a);
System.out.println("b value is " + b);
System.out.println("c value is " + c);
System.out.println("\n(a != b) is " + d);
System.out.println("(a != c) is " + e);
}
}
Output
a value is 6
b value is 6
c value is 3
(a != b) is false
(a != c) is true
Greater Than ( > )
Greater than relational operator is used to verify a value is higher than another value. The greater than relational operator will check relatively higher precedence compare to other operands. If the operands has higher value than another operands then it returns true. Otherwise the greater than relational operator will return false.
package com.yawintutor;
public class RelationalGreaterThan {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = 3;
boolean d = false;
boolean e = false;
d = (a > b);
e = (a > c);
System.out.println("a value is " + a);
System.out.println("b value is " + b);
System.out.println("c value is " + c);
System.out.println("\n(a > b) is " + d);
System.out.println("(a > c) is " + e);
}
}
Output
a value is 5
b value is 6
c value is 3
(a > b) is false
(a > c) is true
Greater Than Or Equals To ( >= )
Greater than or equals to relational operator is used to verify a value is higher than another value or both or equal. The greater than or equals to relational operator is same as the greater than relational operator, in addition to that it checks the equality also. If the first operand is either greater than the other operand or the first operand is equals to the second operand, then it returns true. Otherwise, it returns false.
package com.yawintutor;
public class RelationalGreaterThanOrEqual {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = 3;
int d = 5;
boolean e = false;
boolean f = false;
boolean g = false;
e = (a >= b);
f = (a >= c);
g = (a >= d);
System.out.println("a value is " + a);
System.out.println("b value is " + b);
System.out.println("c value is " + c);
System.out.println("d value is " + d);
System.out.println("\n(a >= b) is " + e);
System.out.println("(a >= c) is " + f);
System.out.println("(a >= d) is " + g);
}
}
Output
a value is 5
b value is 6
c value is 3
d value is 5
(a >= b) is false
(a >= c) is true
(a >= d) is true
Lesser Than ( < )
Lesser than relational operator is used to verify a value is smaller than another value. The lesser than relational operator is opposite to the greater than or equals to operator. If the operand has less precedence compare to the another operand, the lesser than relational operator returns true. Otherwise the lesser than relational operator will return false.
package com.yawintutor;
public class RelationalLessThan {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = 3;
boolean d = false;
boolean e = false;
d = (a < b);
e = (a < c);
System.out.println("a value is " + a);
System.out.println("b value is " + b);
System.out.println("c value is " + c);
System.out.println("\n(a < b) is " + d);
System.out.println("(a < c) is " + e);
}
}
Output
a value is 5
b value is 6
c value is 3
(a < b) is true
(a < c) is false
Lesser Than Or Equals To ( <= )
Lesser than or equals to relational operator is used to verify a value is smaller than another value or both or equal. The lesser than or equals to relational operator is opposite to the greater than relational operator. if the operand value is lesser precedence or equal precedence to another operand, the lesser than or equals to relational operator will return true. Otherwise, it returns false.
package com.yawintutor;
public class RelationalLessThanOrEqual {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = 3;
int d = 5;
boolean e = false;
boolean f = false;
boolean g = false;
e = (a <= b);
f = (a <= c);
g = (a <= d);
System.out.println("a value is " + a);
System.out.println("b value is " + b);
System.out.println("c value is " + c);
System.out.println("d value is " + d);
System.out.println("\n(a <= b) is " + e);
System.out.println("(a <= c) is " + f);
System.out.println("(a <= d) is " + g);
}
}
Output
a value is 5
b value is 6
c value is 3
d value is 5
(a <= b) is true
(a <= c) is false
(a <= d) is true