== operator is a type of Relational Operator in Java which is used to check for relations of equality. It returns a boolean result after the comparison and is extensively used in looping statements as well as conditional if-else statements.
Syntax:
LHS value == RHS value
But, while comparing these values, three cases arise generally:
- Case 1: When both LHS and RHS values are primitive
This is most simple among the cases. As a primitive data is stored in the stack memory, in this case, the actual value of both the sides are 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 program for using == operatorimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args){// Declaring primitive valuesinta =4;intb =4;intc =5;// Comparing a and b using == operatorSystem.out.println("Are "+ a+" and "+ b+" equal? "+ (a == b));// Comparing b and c using == operatorSystem.out.println("Are "+ b+" and "+ c+" equal? "+ (b == c));}}chevron_rightfilter_noneOutput:Are 4 and 4 equal? true Are 4 and 5 equal? false
- Case 2: When one of LHS and RHS value is a primitive and the other is reference
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 valueSo 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 errorThis 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 program for using == operatorimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args){// Declaring primitive valueinta =4;// Declaring reference valueint[] b = {1,2,3,4};// Comparing a and b using == operatorSystem.out.println("Are "+ a+" and "+ b+" equal? "+ (a == b));}}chevron_rightfilter_none - Case 3: When both of the LHS and RHS value are reference
In this scenario, for both the 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, then this operator returns true. Else it returns false.Syntax:
Address value == Address value
Example:
// Java program for using == operatorimportjava.io.*;publicclassGFG {publicstaticvoidmain(String[] args){// Declaring reference valueint[] 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 memorySystem.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 memorySystem.out.println("Are "+ b+" and "+ c+" equal? "+ (b == c));}}chevron_rightfilter_noneOutput:Are [I@232204a1 and [I@4aa298b7 equal? false Are [I@4aa298b7 and [I@4aa298b7 equal? true
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- && operator in Java with Examples
- & Operator in Java with Examples
- Java Unary Operator with Examples
- Java Ternary Operator with Examples
- Java Assignment Operator with Examples
- Diamond operator for Anonymous Inner Class with Examples in Java
- new operator vs newInstance() method in Java
- instanceof operator vs isInstance() method in Java
- new operator in Java
- Java Ternary Operator Puzzle
- Double colon (::) operator in Java
- Difference between concat() and + operator in Java
- || operator in Java
- Multiples of 3 and 5 without using % operator
- How can we use Comma operator in place of curly braces?
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

