Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Relational operators, also known as comparison operators, are used to compare values and return a boolean result. They are useful for making decisions in your program based on the relationship between two values. In Java, the following relational operators are available:
==
(Equal to): Checks if two values are equal.int a = 5; int b = 10; System.out.println(a == b); // Output: false
!=
(Not equal to): Checks if two values are not equal.int a = 5; int b = 10; System.out.println(a != b); // Output: true
<
(Less than): Checks if the value on the left is less than the value on the right.int a = 5; int b = 10; System.out.println(a < b); // Output: true
>
(Greater than): Checks if the value on the left is greater than the value on the right.int a = 5; int b = 10; System.out.println(a > b); // Output: false
<=
(Less than or equal to): Checks if the value on the left is less than or equal to the value on the right.int a = 5; int b = 10; System.out.println(a <= b); // Output: true
>=
(Greater than or equal to): Checks if the value on the left is greater than or equal to the value on the right.int a = 5; int b = 10; System.out.println(a >= b); // Output: false
These relational operators can be used with various data types, such as int
, float
, double
, and char
. However, when comparing objects, you should use the equals()
method instead of the ==
operator to compare their contents. The ==
operator will only compare the references of the objects, not their actual contents.
Example with String
objects:
String str1 = "hello"; String str2 = "world"; String str3 = "hello"; System.out.println(str1.equals(str2)); // Output: false System.out.println(str1.equals(str3)); // Output: true
In this tutorial, we covered the basics of relational operators in Java. These operators allow you to compare values and make decisions in your program based on their relationships.
Comparison Operators in Java:
==
, !=
, <
, >
, <=
, >=
) are used to compare values and return boolean results.int a = 5, b = 10; boolean isEqual = (a == b);
Using Relational Operators in Java:
boolean isGreaterThan = (a > b);
Java Equality and Inequality Operators:
==
) and inequality (!=
) operators compare values for equality.boolean areNotEqual = (a != b);
Comparing Numbers with Relational Operators in Java:
boolean isLessThanOrEqual = (a <= b);
String Comparison using Relational Operators in Java:
equals()
method or relational operators.String str1 = "Hello", str2 = "World"; boolean areStringsEqual = str1.equals(str2);
Logical Operators and Relational Operators in Java:
&&
, ||
, !
) can combine results of relational operators.boolean logicalResult = (a > 0 && b < 20);
Chaining Relational Operators in Java:
boolean complexCondition = (a > 0 && b < 20 || c == 30);
Conditional Statements with Java Relational Operators:
if (a > b) { // Code to execute if condition is true } else { // Code to execute if condition is false }
Java Relational Operators Precedence:
int result = (a + b > c * d);
Java Boolean Expressions and Relational Operators:
boolean booleanExpression = (a > b && c != 0);
Java Relational Operators Examples:
boolean example1 = (x == y); boolean example2 = (name.equals("John"));
Comparing Objects with Relational Operators in Java:
equals()
method or custom comparison logic.Object obj1 = new Object(); Object obj2 = new Object(); boolean areObjectsEqual = obj1.equals(obj2);
Java instanceof
Operator and Relational Operators:
instanceof
operator checks if an object is an instance of a particular class.if (obj instanceof MyClass) { // Code to execute if obj is an instance of MyClass }
Java Bitwise and Relational Operators:
&
, |
, ^
, ~
) and relational operators can be used together.int bitwiseResult = (a > b & c < 10);