Table of Contents
Types of Relational Operators
Below are the two types of relational operators:1. Equality Operators
- == (Equal To): This checks if two values are equal.
- != (Not Equal To): This checks if two values are not equal.
- (Greater Than): This checks if the left value is greater than the right value.
- (Less Than): This checks if the left value is smaller than the right value.
- = (Greater Than or Equal To): This checks if the left value is greater than or equal to the right value.
- = (Less Than or Equal To): This hecks if the left value is less than or equal to the right value.
Equal To Operator (==)
The == operator checks whether two values are equal. It returns true if both operands have the same value.Syntax:
Example:value1 == value2
Below is the Java program to show Equal To operator:
// Java program to show Equal To operator
public class EqualToOperatorExample
{
public static void main(String[] args)
{
int a = 10;
int b = 10;
// Using Equal To Operator
System.out.println(a == b);
}
}
Output:
true
Not Equal To Operator (!=)
The != operator checks whether two values are different. It returns true when the values are not equal.Syntax:
Example:value1 != value2
Below is the Java program to show Not Equal To operator:
// Java program to show Not Equal To operator
public class NotEqualOperatorExample
{
public static void main(String[] args)
{
int a = 10;
int b = 5;
// Using Not Equal To Operator
System.out.println(a != b);
}
}
Output:
true
Greater Than Operator ()
The operator checks if the left value is greater than the right value. It is mainly used in numeric comparisons.Syntax:
value1 value2
Below is the Java program to show Greater Than operator:
// Java program to show Greater Than operator
public class GreaterThanOperatorExample
{
public static void main(String[] args)
{
int a = 15;
int b = 10;
// Using Greater Than Operator
System.out.println(a b);
}
}
Output:
true
Less Than Operator ()
The operator checks if the left value is smaller than the right value. It is commonly used in loops and conditions.Syntax:
Example:value1 value2
Below is the Java program to show Less Than operator:
// Java program to show Less Than operator
public class LessThanOperatorExample
{
public static void main(String[] args)
{
int a = 5;
int b = 10;
// Using Less Than Operator
System.out.println(a b);
}
}
Output:
true
Greater Than or Equal To (=)
The = operator checks whether a value is greater than or equal to another value. It combines comparison and equality.Syntax:
Example:value1 = value2
Below is the Java program to show Greater Than or Equal To operator:
// Java program to show Greater Than
// or Equal To Operator
public class GreaterThanEqualOperatorExample
{
public static void main(String[] args)
{
int a = 10;
int b = 10;
// Using Greater Than or Equal To Operator
System.out.println(a = b);
}
}
Output:
true
Less Than or Equal To (=)
The = operator checks whether a value is less than or equal to another value. It includes boundary values.Syntax:
Example:value1 = value2
Below is the Java program to show Less Than or Equal To operator:
// Java program to show Less Than
// or Equal To Operator
public class LessThanEqualOperatorExample
{
public static void main(String[] args)
{
int a = 8;
int b = 10;
// Using Less Than or Equal To Operator
System.out.println(a = b);
}
}
Output:
true
Relational Operators with Different Data Types
Relational operators can be used with different data types, but their behavior may vary depending on the type.Syntax:
Example:value1 operator value2
Below is the Java program to show relational operator with different data types:
// Java program to show relational operator
// with different data types
public class RelationalDifferentDataTypesExample
{
public static void main(String[] args)
{
// String comparison using compareTo()
System.out.println("Apple".compareTo("Banana") 0);
// Character comparison
System.out.println('A' 'B');
}
}
Output:
true
true
Relational Operator in in Conditional Statements
Relational operators are used in conditions to control the flow of a program.Syntax:
Example:if (condition)
{
// code
}
// Java program to show relational operator
// in conditional statements
public class ConditionalStatementExample
{
public static void main(String[] args)
{
int age = 18;
// Using relational operator in condition
if (age = 18)
{
System.out.println("Eligible to vote");
}
else
{
System.out.println("Not eligible");
}
}
}
Output:
Eligible to vote
Conclusion
Relational operators are essential for comparing values and implementing decision-making logic in Java. They form the foundation of conditional statements and help control program execution.Frequently Asked Questions
1. What is a relational operator?2. What is the difference between = and ==?A relational operator compares two values and returns a boolean result.
3. Can relational operators be used with strings?= is used for assignment, while == is used for comparison.
4. What do relational operators return?Yes, they can be used indirectly using methods like compareTo().
5. Why are relational operators important?They always return true or false.
They are used for decision-making and controlling program flow.
0 Comments