Equality (==) operator in Java with Examples - GeeksforGeeks

24 Jan, 2022
== operator is a type of Relational Operator in Java used to check for relations of equality. It returns a boolean result after the comparison and is extensively used in looping statements and conditional if-else statements. 
Syntax: 
LHS value == RHS value
But, while comparing these values, three cases arise generally:
This is the most simple among the cases. As primitive data is stored in the stack memory, in this case, the actual value of both sides is fetched from the stack memory and compared. It returns true if they both are equal, else false is returned.
Syntax: 
Actual value == Actual value
Example: 
Java
// Java program for using == operator import java.io.*; public class GFG {    public static void main(String[] args)    {        // Declaring primitive values        int a = 4;        int b = 4;        int c = 5;         // Comparing a and b using == operator        System.out.println("Are " + a + " and " + b                           + " equal? " + (a == b));         // Comparing b and c using == operator        System.out.println("Are " + b + " and " + c                           + " equal? " + (b == c));    }}
Are 4 and 4 equal? true
Are 4 and 5 equal? false
In this scenario, for the primitive side, the actual value is taken for comparison from the stack memory. But for the reference side, when an array is declared and initialized, the data is stored in the heap memory and the reference pointer in the stack memory. So all that is in the stack memory is the memory address. 
Syntax: 
Actual value == Address value
    OR
Address value == Actual value
So when the comparison between a primitive value and a reference value is taken, the program doesn’t compile and throws an error:
Compilation Error in java code:- 
prog.java:20: error: incomparable types: int and int[]
                           + (a == b));
                                ^
1 error
This is because the value for the primitive side is easily fetched from the stack memory, but for the reference side, the value cannot be fetched as the value is in heap memory. Hence the error.
Example: 
Java
// Java program for using == operator import java.io.*; public class GFG {    public static void main(String[] args)    {         // Declaring primitive value        int a = 4;         // Declaring reference value        int[] b = { 1, 2, 3, 4 };         // Comparing a and b using == operator        System.out.println("Are " + a + " and " + b                           + " equal? " + (a == b));    }}
prog.java:17: error: bad operand types for binary operator '=='
                           + " equal? " + (a == b));
                                             ^
  first type:  int
  second type: int[]
1 error
In this scenario, for both sides, when an array is declared and initialized, the data is stored in the heap memory and the reference pointer in the stack memory. So both the variables, their address is checked. If both the variables point to the same memory address, this operator returns true. Else it returns false.
Syntax: 
Address value == Address value
Example: 
Java
// Java program for using == operator import java.io.*; public class GFG {    public static void main(String[] args)    {         // Declaring reference value        int[] a = { 1, 2, 3, 4 };        int[] b = { 1, 2, 3, 4 };        int[] c = b;         // Comparing a and b using == operator        // Though they both have the same value        // the output will be false because        // they both have a different address in the memory        System.out.println("Are a and b equal? "                           + (a == b));         // Comparing b and c using == operator        // Though they both have the same value        // the output will be true because        // they both have same address in the memory        System.out.println("Are b and c equal? "                           + (b == c));    }}
Are a and b equal? false
Are b and c equal? true
nishkarshgandhi
Java-Operators
Java
Java-Operators
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java