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

Java Relational Operators

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.

  1. Comparison Operators in Java:

    • Java comparison operators (==, !=, <, >, <=, >=) are used to compare values and return boolean results.
    int a = 5, b = 10;
    boolean isEqual = (a == b);
    
  2. Using Relational Operators in Java:

    • Relational operators compare two values and return a boolean result.
    boolean isGreaterThan = (a > b);
    
  3. Java Equality and Inequality Operators:

    • The equality (==) and inequality (!=) operators compare values for equality.
    boolean areNotEqual = (a != b);
    
  4. Comparing Numbers with Relational Operators in Java:

    • Relational operators are commonly used to compare numeric values.
    boolean isLessThanOrEqual = (a <= b);
    
  5. String Comparison using Relational Operators in Java:

    • String comparison using equals() method or relational operators.
    String str1 = "Hello", str2 = "World";
    boolean areStringsEqual = str1.equals(str2);
    
  6. Logical Operators and Relational Operators in Java:

    • Logical operators (&&, ||, !) can combine results of relational operators.
    boolean logicalResult = (a > 0 && b < 20);
    
  7. Chaining Relational Operators in Java:

    • Chaining multiple relational operators for complex conditions.
    boolean complexCondition = (a > 0 && b < 20 || c == 30);
    
  8. Conditional Statements with Java Relational Operators:

    • Relational operators are commonly used in conditional statements.
    if (a > b) {
        // Code to execute if condition is true
    } else {
        // Code to execute if condition is false
    }
    
  9. Java Relational Operators Precedence:

    • Relational operators have lower precedence than arithmetic operators.
    int result = (a + b > c * d);
    
  10. Java Boolean Expressions and Relational Operators:

    • Boolean expressions are formed using relational operators.
    boolean booleanExpression = (a > b && c != 0);
    
  11. Java Relational Operators Examples:

    • Various examples showcasing the use of relational operators in different scenarios.
    boolean example1 = (x == y);
    boolean example2 = (name.equals("John"));
    
  12. Comparing Objects with Relational Operators in Java:

    • Objects are compared using the equals() method or custom comparison logic.
    Object obj1 = new Object();
    Object obj2 = new Object();
    boolean areObjectsEqual = obj1.equals(obj2);
    
  13. Java instanceof Operator and Relational Operators:

    • The 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
    }
    
  14. Java Bitwise and Relational Operators:

    • Bitwise operators (&, |, ^, ~) and relational operators can be used together.
    int bitwiseResult = (a > b & c < 10);