- A ==
- B !=
- C >
- D <=
The equality operator (==) in Java is used to check if two values are equal. It returns true if the values are equal and false otherwise.
public class EqualityOperatorExample { public static void main(String[] args) { // Example values int number1 = 5; int number2 = 7; // Check if the values are equal if (number1 == number2) { System.out.println("The values are equal."); } else { System.out.println("The values are not equal."); } } }
In this example, number1
and number2
are compared using the equality operator (==
). If the values are equal, it prints "The values are equal"; otherwise, it prints "The values are not equal."
The inequality operator (!=) in Java is used to check if two values are not equal. In this case, 8 != 5 is true because 8 is not equal to 5.
If m
is 8 and n
is 5, the expression m != n
using the inequality operator (!=
) in Java evaluates to true
. This is because 8
is not equal to 5
.
Here's a simple Java program to illustrate this:
public class InequalityOperatorExample { public static void main(String[] args) { // Example values int m = 8; int n = 5; // Check if the values are not equal if (m != n) { System.out.println("The values are not equal."); } else { System.out.println("The values are equal."); } } }
When you run this program, it will print "The values are not equal."
The greater-than operator (>) in Java is used to check if the left operand is greater than the right operand. In this case, 10 > 15 is false because 10 is not greater than 15.
If p
is 10 and q
is 15, the expression p > q
in Java evaluates to false
. This is because 10
is not greater than 15
.
Here's a simple Java program to illustrate this:
public class GreaterThanOperatorExample { public static void main(String[] args) { // Example values int p = 10; int q = 15; // Check if p is greater than q if (p > q) { System.out.println("p is greater than q."); } else { System.out.println("p is not greater than q."); } } }
When you run this program, it will print "p is not greater than q."
If x
is 7 and y
is 7, the expression x >= y
in Java evaluates to true
. This is because 7
is equal to 7
, and the greater-than-or-equal-to operator (>=
) returns true
when the left operand is greater than or equal to the right operand.
Here's a simple Java program to illustrate this:
public class GreaterThanOrEqualOperatorExample { public static void main(String[] args) { // Example values int x = 7; int y = 7; // Check if x is greater than or equal to y if (x >= y) { System.out.println("x is greater than or equal to y."); } else { System.out.println("x is not greater than or equal to y."); } } }
When you run this program, it will print "x is greater than or equal to y."
In Java, the less-than operator (<
) is used to check if one value is less than another. It evaluates to true
if the left operand is less than the right operand; otherwise, it evaluates to false
.
Here's a simple example:
public class LessThanOperatorExample { public static void main(String[] args) { // Example values int a = 5; int b = 10; // Check if a is less than b if (a < b) { System.out.println("a is less than b."); } else { System.out.println("a is not less than b."); } } }
In this example, since a
is 5 and b
is 10, the condition a < b
is true
, and the program prints "a is less than b."
The less-than-or-equal-to operator (<=) in Java is used to check if the left operand is less than or equal to the right operand. In this case, 6 <= 6 is true because 6 is equal to 6.
If a
is 6 and b
is 6, the result of the expression a <= b
using the less-than-or-equal-to operator (<=
) in Java would be true
. The less-than-or-equal-to operator evaluates to true
if the left operand is less than or equal to the right operand.
Here's a simple example:
public class LessThanOrEqualToOperatorExample { public static void main(String[] args) { // Example values int a = 6; int b = 6; // Check if a is less than or equal to b boolean result = a <= b; // Print the result System.out.println("Is a less than or equal to b? " + result); } }
In this case, the output will be:
Is a less than or equal to b? true
The less-than operator (<) in Java is used to check if the left operand is less than the right operand. In this case, 9 < 4 is true because 9 is less than 4.
If x
is 9 and y
is 4, the result of the expression x < y
using the less-than operator (<
) in Java would be false
. The less-than operator evaluates to true
if the left operand is less than the right operand, and in this case, 9 is not less than 4.
Here's a simple example:
public class LessThanOperatorExample { public static void main(String[] args) { // Example values int x = 9; int y = 4; // Check if x is less than y boolean result = x < y; // Print the result System.out.println("Is x less than y? " + result); } }
In this case, the output will be:
Is x less than y? false
If p
is 10 and q
is 10, the result of the expression p == q
using the equality operator (==
) in Java would be true
. The equality operator checks if the values on both sides are equal, and in this case, both p
and q
have the value of 10.
Here's a simple example:
public class EqualityOperatorExample { public static void main(String[] args) { // Example values int p = 10; int q = 10; // Check if p is equal to q boolean result = p == q; // Print the result System.out.println("Is p equal to q? " + result); } }
In this case, the output will be:
Is p equal to q? true
If m
is 7 and n
is 5, the result of the expression m <= n
using the less-than-or-equal-to operator (<=
) in Java would be false
. The expression checks if the value on the left (m
in this case) is less than or equal to the value on the right (n
).
Here's an example:
public class LessThanOrEqualToOperatorExample { public static void main(String[] args) { // Example values int m = 7; int n = 5; // Check if m is less than or equal to n boolean result = m <= n; // Print the result System.out.println("Is m less than or equal to n? " + result); } }
In this case, the output will be:
Is m less than or equal to n? false
If x
is 12 and y
is 18, the result of the expression x != y
using the inequality operator (!=
) in Java would be true
. The expression checks if the values on the left (x
in this case) and on the right (y
) are not equal.
Here's an example:
public class InequalityOperatorExample { public static void main(String[] args) { // Example values int x = 12; int y = 18; // Check if x is not equal to y boolean result = x != y; // Print the result System.out.println("Is x not equal to y? " + result); } }
In this case, the output will be:
Is x not equal to y? true