Relational Operators in Java – With Code Examples

Updated on October 23, 2024

Article Outline

In Java programming, relational operators are fundamental because they enable the comparison of values and decisions based on those companies. These operators are sufficient for managing control in flows within the application, and to form conditions for loops, if statements and other control constructs. This article focuses on the different relational operators, which are available in Java language, their syntax as well as the practical syntax.

What are Relational Operators?

Java uses the relational operators to compare two values and depending upon their conditions controls the flow of the program. The operators offered here include !=, >, <, >=, and <= operators for testing between two operands. What if you wanted a == check or != check? The == operator checks for equality and, the != operator is used to check if two values are different. From these comparisons get boolean values to be able to decide and decide by if statements and loops in the source code. Developers can make more dynamic and responsive applications, applications that respond to user input or data states, using related operators.

*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Syntax

The syntax for relational operators in Java is given below.

 

operand1 relational_operator operand2

 

  • operand1: It is the first variable that to be compared.
  • operand2: It is the second variable to be compared.
  • Relational_operator: The relational operator checks the relation between operand1 and operand2.

Types of Relational Operators in Java

 

There are six types of relational operators in Java language. Let’s discuss each one by one.

 

  • Equal To (==): This operator checks whether two operands are equal or not.
  • Not Equal To (!=): Checks if two operands are not equal.
  • Greater Than (>): This operator checks whether one operand is greater than another or not.
  • Less Than (<): This operator checks if one operand is less than another.
  • Greater Than or Equal to (>=): Checks if one operand is either greater than or equal to the other.
  • Less than or equal to (<=): Checks if one operand is either less than or equal to the other.

 

Also Read: Java Operators

 

Equal To (==)

This operator checks if two operands are equal. It returns true if they are equal; otherwise, it returns false.

 

Syntax of equal operator

operand1 ==  operand2

Not Equal To (!=)

This operator checks if two operands are not equal. It returns true if two operands are not equal. Otherwise, it will return false.

 

Syntax of not equal operator

operand1 != operand2

 

Greater Than (>)

 This operator makes a check if one of the operands is less than the other side. If the first operand is less than if true and if false, otherwise, it will return false.

 

Syntax

 operand1 > operand2

 

Less Than (<)

This operator checks if one operand is less than the other operator. This operator returns true if the first operand is less than otherwise, and it will return false.

 

Syntax of < operator is

operand1 < operand2

Greater than or Equal to (>=)

A greater than or equal to check of one operand with another. It returns true if the first operand is greater than or equal to the second and false otherwise.

 

Syntax of >= operator is

 operand1 < operand2

Less Than or Equal To (<=)

This operator checks if one operand is less than or equal to its operand. The function returns True if the first operand is less or equal to the other. It is false.

 

The syntax for the <= operator

 operand1 <= operand2

Examples of Relational Operators in Java

Also read: Bitwise Operators in Java

 

Equal To (==)

 

The following program demonstrates that it is equal to the operator.

 

Program

class Main{ public static void main(String args[]){ System.out.println("30 == 30 " +(30==30)) ; System.out.println("d==d "+('b'=='b')) ; System.out.println("1.2 == 1.2 "+(1.2 == 1.2)) ; System.out.println("5 == 9 "+ (1== 3)); } }

.
Output

30 == 30 true d==d true 1.2 == 1.2 true 5 == 9 false

Also Read: Ternary Operator Java

Not Equal To (!=)

 

The following program demonstrates the != operator to check if the two values are unequal.

Program

public class Main { public static void main(String[] args) { System.out.println("20 != 20: " + (20 != 20)); System.out.println("r != m: " + ('r' != 'm')); System.out.println("1.3 != 1.3: " + (1.3 != 1.3)); System.out.println("2 != 2: " + (2 != 2)); } }

Output

20 != 20: false r != m: true 1.3 != 1.3: false 2 != 2: false

Greater Than (>)

 

We used the > operator to check if one value is greater than the other.

public class Main{ public static void main(String args[]){ System.out.println("5>3"+(5>3)); System.out.println("b> a"+('b' >'a')) ; System.out.println("1> 2"+ (1>2)); } }

Output

5>3true b> atrue 1> 2false

Less Than (<)

 

The following program demonstrates the less-than-operator.

public class Main { public static void main(String[] args) { System.out.println("29 < 3: " + (29 < 3)); System.out.println("a <  b: " + ('a' < 'b')); System.out.println("25 < 5: " + (25 < 5)); } }

Output

29 < 3: false a <  b: true 25 < 5: false

Also Read: Logical Operators in Java

 

Greater Than or Equal To (>=)

 

The following program demonstrates the greater than or equal to operator.

 

Program

public class Main { public static void main(String[] args) { System.out.println("29 >= 2: " + (3 >= 3)); System.out.println("30 >= 2: " + (5 >= 3)); } }

Output

29 >= 2: true 30 >= 2: true

Less Than or Equal to (<=)

 

The following program demonstrates the less than or equal to.

 

Program

public class Main { public static void main(String[] args) { System.out.println("20 <= 5: " + (20 <= 5)); System.out.println("30 <= 2: " + (30 <= 2)); } }

Output

20 <= 5: false 30 <= 2: false

Characteristics of Relational Operators

In this section, we will see the characteristics of Relational Operators.

 

  • Non-associative: The relational operators are non-associative, meaning your cannot chain them directly (e.g. a<b< c should be interpreted carefully, use parentheses for clarity).
  • Short-Circuiting: It is used in conjunction with logical operators (&& , || ). Java evaluates expressions from left to right and may skip evaluating the second operand if the result can be determined from the first.
  • Performance: The comparison is efficient, but care should be taken with complex objects or collections.
  • Precedence: The relational operators have lower precedence than arithmetic operators but higher than logical operators.
  • No Implicit Type Coercion: Java language does not perform implicit type coercion in relational comparisons, ensuring type safety.

 

Also Read: Java Interview Questions and Answers

Conclusion

Relational operators are one of the most important aspects of Java programming that enable value and expression comparison. As your usability operators for checking equality, inequality and relative magnitude these are the main operators for decision-making in your source code. They are very and can be used with any type of data, and they can also be used to control the flow of the program using structures like a loop or conditional statements.

 

Having a grasp of how to use relational operators makes you build more logical programs, efficient, and responsive in solving various problems than when you don’t. Before setting out to write any program in Java, it is crucial for someone intending to become a Java developer to have a good understanding of relational operators. Want to master Java? Check out Hero Vired’s Certificate Program in Application Development.

FAQs
Although the relational operators cannot be directly used with boolean values, they can be used within logical expressions that combine boolean results.
Relational operations may be used in control flow statements like an if, while and for loop to make tests and comparisons.
Relational operators can be combined with logical operators (&&, ||, !), yielding upon more complex conditional expressions.
Yes, relational operators are commonly used in loop conditions (e.g., for a while) to control the iteration based on comparing values

Updated on October 23, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved