Equal Operator (==) in Java

Equal operator in Java with Examples | In Java equal operator is used to compare two primitives based on their value and two objects based on their references. After comparison, it gives a boolean result either true/false. It is widely used in conditional statements. Syntax:-

LHS value == RHS value

Important points on Java Equal operator in Java,

  1. Equal operator can be used to compare both primitive values, and objects.
  2. Equal operator compares primitives based on their values, and objects based on their reference.
  3. The equal (==) operator can’t compare incompatible objects/values, for this we get compile-time error.
  4. It is an operator and in Java we can’t be override operators.

Example to demonstrate equal operator in Java for primitive values,

public class Test {
  public static void main(String[] args) {
    
    int a = 5;
    int b = 10;
    int c = 5;
    
    System.out.println(a == b); // false
    System.out.println(a == c); // true
    System.out.println(b == c); // false
    
    System.out.println('A' == 'A'); // true
    System.out.println('A' == 'B'); // false
    
    System.out.println(true == true); // true
    System.out.println(false == true); // false
  }
}

Output:-

false
true
false
true
false
true
false

The equal (==) operator can’t compare incompatible values, for this we get the compile-time error. For example- boolean is a primitive data type but it is incompatible with remaining all other remaining data types.

public class Test {
  public static void main(String[] args) {
    
    int a = 5;
    double d = 20.5;
    boolean b = true;
    
    System.out.println(a == b);
    System.out.println(d == b);
  }
}

After compilation:-

Test.java:8: error: incomparable types: int and boolean
System.out.println(a == b);
^
Test.java:9: error: incomparable types: double and boolean
System.out.println(d == b);
^
2 errors

While comparing referenced variables, if both variables are referencing to the same object then it gives the result as true, else it gives the result false.

public class Test {
  public static void main(String[] args) {
    
    int[] arr1 = {10, 20, 30, 40, 50};
    int[] arr2 = {10, 20, 30, 40, 50};
    int[] arr3 = arr1;
    
    System.out.println(arr1 == arr2); // false
    System.out.println(arr1 == arr3); // true
  }
}

Output:-

false
true

If objects are pointing from the same referenced variable then they are equal and the equal operator returns true else it returns false.

public class Test {
  public static void main(String[] args) {
    
    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = t1;
    
    System.out.println(t1 == t2); // false
    System.out.println(t1 == t3); // true
  }
}

Output:-

false
true

If we want to compare objects based on their content but not based on their reference then we have to use the equals() method of java.lang.Object class. Also see:- Difference Between == and equals() in Java.

Using an equal operator in Java, We can’t compare incompatible objects else we will get a compile-time error.

public class Test {
  public static void main(String[] args) {
    int[] arr = {10, 20, 30, 40, 50};
    Test t1 = new Test();
    System.out.println(arr == t1); // error
  }
}

After compilation:-

Test.java:5: error: incomparable types: int[ ] and Test
System.out.println(arr == t1); // error
^
1 error

Comparison using ==
Operator
Valid?Return
Two objectsYesfalse
Two primitive variablesYesfalse/true
Two nullsYestrue
Null with objectYesfalse
Object with nullYesfalse
Null referenced with nullYestrue
Null referenced with objectYesfalse
Object with null referencedYesfalse
Incompatible objectsNoerror

Comparing Null using Equal Operator in Java

1) If we compare two nulls with each other using an equal operator then it gives true. Example:-

public class Test {
  public static void main(String[] args) {
    System.out.println(null == null); // true
  }
}

Output:-

true

2) If we compare null with null reference variable using equal operator then also it gives true. Example:-

public class Test {
  public static void main(String[] args) {
    int[] arr = null;
    Test t1 = null;
    System.out.println(arr == null); // true
    System.out.println(t1 == null); // true
  }
}

Output:-

true
true

3) If we compare null with object reference variable using equal operator then it gives false. Example:-

public class Test {
  public static void main(String[] args) {
    int[] arr = {10, 20, 30, 40, 50};
    Test t1 = new Test();
    System.out.println(arr == null); // false
    System.out.println(t1 == null); // false
  }
}

Output:-

false
false

4) If we compare null with Objects using an equal operator then also it gives false. Example:-

public class Test {
  public static void main(String[] args) {
    int[] arr = {10, 20, 30, 40, 50};
    Test t1 = new Test();
    System.out.println(null == new Test()); // false
  }
}

Output:-

false

Using == Operator to Compare null withReturn value
Another nullTrue
Null reference variableTrue
Object reference variableFalse
Objectfalse

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *