Relational operators in java
previous NextRelational operators in Java are used to compare two variables for equality, non-equality, greater than, less than, etc. The Java relational operator always returns a boolean value - that is, true or false.Returns this Boolean value when the conditional statement is used for-ex if,else and elseif, Because relation operators are used to with conditional statement.
java has 6 relation operators
- (==) double equal to operator in java.
- (!=) non equality operator in java.
- (<) less than operator in java.
- (>) greater than operator in java.
- (<=) less than or equal to operator in java.
- (>=) greater than or equal to operator in java.
Double equals to operator in java
We use the double equal then operator to compare the equal value of two different variables.
Example
import java.util.Scanner;
class Relation{
public static void main(String agrs[]){
Scanner scr = new Scanner(System.in);
int x = scr.nextInt();
int y = scr.nextInt();
if(x==y){
System.out.println("x and y values are equal");
}
else{
System.out.println("x and y values are not equal");
}
}
}
Output :
40 //x value 40 //y value x and y value is equal
Non equality operator in java
It returns true if both the operands are referring to the different objects, otherwise false.< is less than operator.
Example
import java.util.Scanner;
class Relation{
public static void main(String agrs[]){
Scanner scr = new Scanner(System.in);
int a = scr.nextInt();
int b = scr.nextInt();
if(a!=b){
System.out.println("x and y values are equal");
}
else
{
System.out.println("x and y values are not equal");
}
}
}
Output :
45 12 x and y values are equal
Less than operator in java
Two variables are compared whether the first variable is greater or smaller than the second variable.In less than operator, but the value of variable1 is less than that of variable2(var1<)
Example
import java.util.Scanner;
class Relation{
public static void main(String agrs[]){
Scanner scr = new Scanner(System.in);
int x = scr.nextInt();
int y = scr.nextInt();
if(x>y){
System.out.println("x is greater");
}else{
System.out.println("z is greater");
}
}
}
Output :
40 //x value 10 //y value x is greater
Greater than operator in java
Two variables are compared whether the first variable is greater or smaller than the second variable.In greater than operator, the value of variable1 is greater than that of variable2(var1>var2).
Example
import java.util.Scanner;
class Relation{
public static void main(String agrs[]){
Scanner scr = new Scanner(System.in);
int x = scr.nextInt();
int y = scr.nextInt();
if(x<y){
System.out.println("y is greater");
}else if(x>y){
System.out.println("x is greater");
}else{
System.out.println("x and y are equels");
}
}
}
Output :
40 //x value 56 //y value y is greater
Less than or equal to operator in java
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B)
Example
import java.util.Scanner;
class Relation{
public static void main(String agrs[]){
Scanner scr = new Scanner(System.in);
int a = scr.nextInt();
int b = scr.nextInt();
if(a<=b){
System.out.println("condition true");
}else{
System.out.println("condition false");
}
}
}
Output :
45 // value of a 50 // value of b condition true

Greater than or equals operator in java
Checks if the value of right operand is less than or equal to the value of left operand, if yes then condition becomes true. (A <= B) is true.
Example
import java.util.Scanner;
class Relation{
public static void main(String agrs[]){
Scanner scr = new Scanner(System.in);
int a = scr.nextInt();
int b = scr.nextInt();
if(a>=b){
System.out.println("condition true");
}
else
{
System.out.println("condition false");
}
}
}
Output :
25 // value of a 50 // value of b condition false