true
or false
).
int a = 5, b = 5;
System.out.println(a == b); // Output: true
int a = 5, b = 10;
System.out.println(a != b); // Output: true
int a = 5, b = 10;
System.out.println(a < b); // Output: true
int a = 10, b = 5;
System.out.println(a > b); // Output: true
int a = 5, b = 10;
System.out.println(a <= b); // Output: true
int a = 10, b = 10;
System.out.println(a >= b); // Output: true
public class RelationalOperators
{
public static void main(String[] args)
{
// Initialize variables
int a = 10, b = 20;
double x = 15.5, y = 15.5;
// Equal To (==)
System.out.println("Equal To (a == b): " + (a == b)); // Output: false
System.out.println("Equal To (x == y): " + (x == y)); // Output: true
// Not Equal To (!=)
System.out.println("Not Equal To (a != b): " + (a != b)); // Output: true
System.out.println("Not Equal To (x != y): " + (x != y)); // Output: false
// Less Than (<)
System.out.println("Less Than (a < b): " + (a < b)); // Output: true
System.out.println("Less Than (x < y): " + (x < y)); // Output: false
// Greater Than (>)
System.out.println("Greater Than (a > b): " + (a > b)); // Output: false
System.out.println("Greater Than (x > y): " + (x > y)); // Output: false
// Less Than or Equal To (<=)
System.out.println("Less Than or Equal To (a <= b): " + (a <= b)); // Output: true
System.out.println("Less Than or Equal To (x <= y): " + (x <= y)); // Output: true
// Greater Than or Equal To (>=)
System.out.println("Greater Than or Equal To (a >= b): " + (a >= b)); // Output: false
System.out.println("Greater Than or Equal To (x >= y): " + (x >= y)); // Output: true
}
}
Equal To (a == b): false Equal To (x == y): true Not Equal To (a != b): true Not Equal To (x != y): false Less Than (a < b): true Less Than (x < y): false Greater Than (a > b): false Greater Than (x > y): false Less Than or Equal To (a <= b): true Less Than or Equal To (x <= y): true Greater Than or Equal To (a >= b): false Greater Than or Equal To (x >= y): true
boolean
.
int a = 10, b = 20; boolean result = a < b; // true
.
true
or false
).
5 > 3
evaluates to true
.
(0.1 + 0.2) == 0.3; // false due to floating-point representation errors
.
<
, >
, etc.) cannot be used with String objects; use compareTo for lexicographical comparison.
"abc".compareTo("def") < 0 // true
.
==
checks reference equality (if two references point to the same object), not content equality.
.equals()
to compare the content of objects.
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2); // true (same reference in the string pool)
System.out.println(s1.equals(s2)); // true (content is the same)
Arrays.equals()
or iterate through elements.
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
System.out.println(arr1 == arr2); // false (different references)
&&
, ||
) but lower precedence than arithmetic operators (+, *).
3 + 5 > 7 && 2 < 4; // true
.
a < b < c
.
10 < 20 < 30; // Compilation error
.
int
, float
, etc.). Implicit casting applies to the smaller type.
int a = 5; double b = 5.0; System.out.println(a == b); // true
.
null
:
==
or !=
cannot be used with null
.
String s = null;
System.out.println(s == null); // true
System.out.println(s < "Java"); // Compilation error
char
values based on their Unicode values.
'a' < 'b' // true
(Unicode 97 < 98).
if
, while
, for
and switch
conditions to determine the flow of execution.
if (a >= b) { /* logic */ }
.
double
and float
, NaN
(Not a Number) is not equal to any value, including itself.
double value = Double.NaN;
System.out.println(value == value); // false
Integer
, Double
, etc.).
Integer a = 100, b = 100;
System.out.println(a == b); // true (cached values)
==
and !=
are valid for boolean values; other relational operators cannot be applied.
boolean flag = true;
System.out.println(flag == true); // true
System.out.println(flag > true); // Compilation error
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.