Table of contents | |
Introduction | |
Relational Operators in Java | |
Examples and Explanation | |
Sample Problems | |
Conclusion |
Relational operators in Java are used to compare two values or expressions and determine the relationship between them. These operators are essential for making decisions and controlling the flow of a program. In this article, we will explore the various relational operators available in Java, understand their usage, and provide examples to illustrate their functionality.
Java provides six relational operators:
Let's dive into some code examples to understand how these relational operators work in practice:
Example 1: Equal to (==) operator
int x = 5;
int y = 7;
boolean result = (x == y);
System.out.println(result);
Output
false
In this example, we compare the values of x and y using the equal to operator (==). Since x is not equal to y, the result is false.
Example 2: Not equal to (!=) operator
int a = 10;
int b = 10;
boolean result = (a != b);
System.out.println(result);
Output
false
In this example, we compare the values of a and b using the not equal to operator (!=). Since a is equal to b, the result is false.
Example 3: Greater than (>) operator
int p = 15;
int q = 10;
boolean result = (p > q);
System.out.println(result);
Output
true
In this example, we compare the values of p and q using the greater than operator (>). Since p is greater than q, the result is true.
Example 4: Less than (<) operator
int m = 5;
int n = 8;
boolean result = (m < n);
System.out.println(result);
Output
true
In this example, we compare the values of m and n using the less than operator (<). Since m is less than n, the result is true.
Example 5: Greater than or equal to (>=) operator
int c = 10;
int d = 10;
boolean result = (c >= d);
System.out.println(result);
Output
true
In this example, we compare the values of c and d using the greater than or equal to operator (>=). Since c is equal to d, the result is true.
Example 6: Less than or equal to (<=) operator
int i = 7;
int j = 10;
boolean result = (i <= j);
System.out.println(result);
Output
true
In this example, we compare the values of i and j using the less than or equal to operator (<=). Since i is less than j, the result is true.
Now that we have understood the basics of relational operators, let's solve a few sample problems:
Problem 1: Write a Java program to check if a given number is even.
int number = 8;
boolean isEven = (number % 2 == 0);
System.out.println("Is the number even? " + isEven);
Output
Is the number even? true
Problem 2: Write a Java program to check if a student's grade is passing (greater than or equal to 60).
int grade = 75;
boolean isPassing = (grade >= 60);
System.out.println("Is the grade passing? " + isPassing);
Output
Is the grade passing? true
Relational operators in Java are powerful tools for comparing values and making decisions within a program. By using these operators, you can evaluate conditions and control the flow of your code. Remember to use the appropriate relational operator based on your comparison requirements. Practice with different examples and sample problems to strengthen your understanding of these operators.
60 videos|37 docs|12 tests
|
|
Explore Courses for EmSAT Achieve exam
|