Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, relational operators are used to compare two values or expressions. They return a boolean value, either true or false, based on the comparison. Here are the relational operators available in Java:

1. Equality Operator (==)

The equality operator is used to compare two values to see if they are equal.

  • Syntax: a == b
  • Example
				
					int x = 5;
int y = 5;
if (x == y) {
    System.out.println("x and y are equal");
}

				
			

Output:  ‘x and y are equal'

2. Inequality Operator (!=)

The inequality operator is used to compare two values to see if they are not equal.

  • Syntax:  ‘a != b
  • Example:
				
					int x = 5;
int y = 10;
if (x != y) {
    System.out.println("x and y are not equal");
}

				
			

Output:   ‘x and y are not equal

3. Greater Than Operator (>)

The greater than operator checks if the value on the left is greater than the value on the right.

  • Syntax:   ‘a != b
  • Example:
				
					int x = 10;
int y = 5;
if (x > y) {
    System.out.println("x is greater than y");
}

				
			

Output: x is greater than y

4. Less Than Operator (<)

The less than operator checks if the value on the left is less than the value on the right.

  • Syntax:  a < b
  • Example:
				
					int x = 5;
int y = 10;
if (x < y) {
    System.out.println("x is less than y");
}

				
			

Output:  x is less than y'

5. Greater Than or Equal To Operator (>=)

The greater than or equal to operator checks if the value on the left is greater than or equal to the value on the right.

  • Syntax: a >= b
  • Example:
				
					int x = 10;
int y = 10;
if (x >= y) {
    System.out.println("x is greater than or equal to y");
}

				
			

Output:  ‘x is greater than or equal to y

6. Less Than or Equal To Operator (<=)

The less than or equal to operator checks if the value on the left is less than or equal to the value on the right.

  • Syntax: a <= b
  • Example:
				
					int x = 5;
int y = 10;
if (x <= y) {
    System.out.println("x is less than or equal to y");
}

				
			

Output: x is less than or equal to y

Using Relational Operators with Different Data Types

Relational operators can be used with various data types such as int, float, char, and double. They can also be used with objects that are comparable, like String.

Example with char:

				
					char a = 'A';
char b = 'B';
if (a < b) {
    System.out.println("a is less than b");
}

				
			

Output:  ‘a is less than b

Example with   ‘double‘:

				
					double x = 5.5;
double y = 2.2;
if (x > y) {
    System.out.println("x is greater than y");
}

				
			

Output:  x is greater than y

 

Using Relational Operators in Conditional Statements

Relational operators are often used in conditional statements such as ‘if‘, ‘else if‘, ‘while‘, and ‘for‘ loops.

Example in a ‘ for‘ loop:

				
					for (int i = 0; i < 5; i++) {
    System.out.println("i is less than 5: " + (i < 5));
}

				
			

Output:

				
					i is less than 5: true
i is less than 5: true
i is less than 5: true
i is less than 5: true
i is less than 5: true

				
			

Important Points

  1. Precision Issues: When comparing floating-point numbers (e.g., float, double), be mindful of precision issues due to how these numbers are represented in memory.
  2. Use .equals() for Objects: For comparing objects (like String), use the .equals() method instead of ==, as == compares references, not values.

Example with String:

				
					String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1.equals(str2)) {
    System.out.println("str1 is equal to str2");
}

				
			

Output:  ‘str1 is equal to str2

Conclusion

Relational operators in Java are fundamental for controlling the flow of a program based on comparisons. They are simple yet powerful tools used extensively in conditions and loops to make decisions based on variable values.

Scroll to Top