Java > Core Java > Operators and Expressions > Relational Operators
Comparing Integers with Relational Operators
This snippet demonstrates the use of relational operators (==, !=, >, <, >=, <=) to compare two integer variables in Java. The output displays the result of each comparison as a boolean value (true or false).
Code Example
This code defines two integer variables, num1
and num2
, and then uses relational operators to compare them. The results of these comparisons are printed to the console. Each System.out.println()
statement displays a string description of the comparison, followed by the boolean result of that comparison enclosed in parentheses. For instance, (num1 == num2)
evaluates to false
because 10 is not equal to 5.
public class RelationalOperatorsExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
System.out.println("num1 == num2: " + (num1 == num2)); // Equal to
System.out.println("num1 != num2: " + (num1 != num2)); // Not equal to
System.out.println("num1 > num2: " + (num1 > num2)); // Greater than
System.out.println("num1 < num2: " + (num1 < num2)); // Less than
System.out.println("num1 >= num2: " + (num1 >= num2)); // Greater than or equal to
System.out.println("num1 <= num2: " + (num1 <= num2)); // Less than or equal to
}
}
Concepts Behind the Snippet
Relational operators are fundamental in programming for making decisions and controlling program flow. They always return a boolean value (true
or false
). These operators are essential for creating conditional statements (if
, else if
, else
) and loops (for
, while
, do-while
).
Real-Life Use Case
Relational operators are used extensively in various real-life scenarios. For example, in an e-commerce application, they might be used to check if the quantity of an item in stock is greater than or equal to the quantity requested by the user before processing an order. Or in a video game, they determine if a player's score is greater than a high score to update the leaderboard. In banking systems, they are used to verify account balances before processing transactions. In short, their usage is broad and applicable to any context that necessitates comparison.
Best Practices
.equals()
method rather than ==
, unless you intend to check for object identity (if they are the same object in memory).=
) assigns a value to a variable, while the equality operator (==
) compares two values for equality. Using the wrong operator can lead to unexpected results.
Interview Tip
Be prepared to explain the difference between ==
and .equals()
, especially when comparing objects (non-primitive types). The ==
operator checks if two object references point to the same memory location, while the .equals()
method (when properly implemented) compares the content of the objects.
When to use them
Use relational operators when you need to compare two values and determine their relationship. This is common in conditional statements (if-else) and loops to control the flow of your program based on certain conditions.
Memory footprint
Relational operators themselves do not directly consume significant memory. The memory footprint is primarily determined by the data types being compared (e.g., int
, double
, String
). The result of a relational operation (true
or false
) occupies a small amount of memory, typically 1 bit, but is usually stored as a byte in most systems.
FAQ
-
What is the difference between == and .equals() in Java?
The==
operator checks if two variables point to the same memory location. For primitive types, it compares the values directly. The.equals()
method, on the other hand, compares the content of two objects. It's crucial to override the.equals()
method in your custom classes to provide meaningful content comparison. -
Can I use relational operators to compare Strings?
While you can use==
to compare Strings, it's generally not recommended.==
checks if the two String variables refer to the same String object in memory. To compare the actual content of two Strings, you should always use the.equals()
method. For example,string1.equals(string2)
.