Relational Operators
Relational operators in Java are used to compare two values. They return a boolean result, which is either true or false. Relational operators are fundamental for controlling the flow of a program using conditional statements such as if, while, and for.
List of Relational Operators #
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Examples and Explanations #
1. Equal to (==)
The equal to operator checks if two values are equal.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
boolean result = (a == b);
System.out.println("a == b: " + result);
b = 10;
result = (a == b);
System.out.println("a == b: " + result);
}
}
Output:
a == b: false
a == b: true
2. Not equal to (!=)
The not equal to operator checks if two values are not equal.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
boolean result = (a != b);
System.out.println("a != b: " + result);
b = 10;
result = (a != b);
System.out.println("a != b: " + result);
}
}
Output:
a != b: true
a != b: false
3. Greater than (>)
The greater than operator checks if the value on the left is greater than the value on the right.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
boolean result = (a > b);
System.out.println("a > b: " + result);
a = 30;
result = (a > b);
System.out.println("a > b: " + result);
}
}
Output:
a > b: false
a > b: true
4. Less than (<)
The less than operator checks if the value on the left is less than the value on the right.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
boolean result = (a < b);
System.out.println("a < b: " + result);
a = 30;
result = (a < b);
System.out.println("a < b: " + result);
}
}
Output:
a < b: true
a < b: false
5. Greater than or equal to (>=)
The greater than or equal to operator checks if the value on the left is greater than or equal to the value on the right.
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 20;
boolean result = (a >= b);
System.out.println("a >= b: " + result);
a = 10;
result = (a >= b);
System.out.println("a >= b: " + result);
}
}
Output:
a >= b: true
a >= b: false
6. Less than or equal to (<=)
The less than or equal to operator checks if the value on the left is less than or equal to the value on the right.
public class Main {
public static void main(String[] args) {
int a = 20;
int b = 20;
boolean result = (a <= b);
System.out.println("a <= b: " + result);
a = 30;
result = (a <= b);
System.out.println("a <= b: " + result);
}
}
Output:
a <= b: true
a <= b: false
Summary
Relational operators are crucial in making comparisons between values in Java. They help in decision-making processes within programs by providing boolean results that control the flow of execution. Understanding how to use these operators effectively allows you to write more dynamic and responsive code.