Relational (Comparison) Operators in Java
×


Relational (Comparison) Operators in Java

225

Relational (Comparison) Operators in Java

In Java, relational operators—also known as comparison operators—are used to compare two values or expressions. These operators return a boolean result: either true or false depending on whether the comparison is valid.

They are essential when writing conditions in if statements, loops, and other control structures.

List of Relational Operators in Java

Java provides six relational operators:

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Equal To (==)

The == operator checks if two values are exactly equal.

int a = 10;
int b = 10;
System.out.println(a == b);  // Output: true
  

Not Equal To (!=)

This operator returns true if the values being compared are not equal.

int x = 5;
int y = 8;
System.out.println(x != y);  // Output: true
  

Greater Than (>)

Checks whether the left operand is greater than the right operand.

int age = 25;
System.out.println(age > 18);  // Output: true
  

Less Than (<)

Returns true if the left operand is smaller than the right.

int marks = 45;
System.out.println(marks < 50);  // Output: true
  

Greater Than or Equal To (>=)

This operator checks whether the left operand is either greater than or equal to the right.

int salary = 30000;
System.out.println(salary >= 30000);  // Output: true
  

Less Than or Equal To (<=)

Returns true if the left operand is less than or equal to the right operand.

int items = 9;
System.out.println(items <= 10);  // Output: true
  

Using Relational Operators in Conditions

These operators are widely used in decision-making constructs such as if, while, and for. Here's an example:

int temp = 40;
if(temp > 35) {
    System.out.println("It's a hot day.");
} else {
    System.out.println("Weather is normal.");
}
// Output: It's a hot day.
  

Working with Different Data Types

Relational operators can also be used with char and float types, not just int.

char c1 = 'A';
char c2 = 'B';
System.out.println(c1 < c2);  // Output: true (Because 'A' has lower ASCII value than 'B')

float f1 = 5.5f;
float f2 = 6.2f;
System.out.println(f1 != f2);  // Output: true
  

Conclusion

Relational (Comparison) Operators in Java form the backbone of logical decision-making. Whether it's checking if a value meets a certain condition, comparing input, or controlling loops, these operators are everywhere.

Mastering their use is a critical step in becoming a confident Java developer.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat