Equality (==) operator in Java with Examples
Last Updated :
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:
Case 1: When both LHS and RHS values are primitive
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));
}
}
OutputAre 4 and 4 equal? true
Are 4 and 5 equal? false
Case 2: When one of the LHS and RHS values is primitive, and the other is a 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 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
Case 3: When both of the LHS and RHS values are reference
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));
}
}
OutputAre a and b equal? false
Are b and c equal? true
Similar Reads
EnumMap equals() Method in Java with Examples The Java.util.EnumMap.equals(obj) in Java is used to compare the passed object with this EnumMap for the equality. It must be kept in mind that the object passed must be a map of the same type as the EnumMap.Syntax: boolean equals(Object obj) Parameter: The method takes one parameter obj of Object t
2 min read
Java String equals() Method String equals() method in Java compares the content of two strings. It compares the value's character by character, irrespective of whether two strings are stored in the same memory location. The String equals() method overrides the equals() method of the object class. false if any of the characters
2 min read
Check if Two Integers are Equal or Not in Java Checking two integers equal or not in Java is done by various approaches. Arithmetic operatorComparison OperatorsString functionsXOR operatorComplement (~) and bit-wise (&) operator Example Input: FirstNumber = 15 SecondNumber= 15 Output: Numbers are same Input: FirstNumber = 15 SecondNumber= 25
2 min read
Period equals() method in Java with Examples The equals() method of Period class in Java is used to check if two given periods are equal or not. The comparison is based on the type Period and each of the three years, months and date. To be equal, all of the three year, month and date must be individually equal. Syntax: public boolean equals(Pe
2 min read
Path equals() method in Java with Examples The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di
2 min read
Short equals() method in Java with Examples The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method. Syntax ShortObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the insta
2 min read
Java Assignment Operators with Examples Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read
Java Arithmetic Operators with Examples Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Float equals() method in Java with examples The equals() method in Float Class is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Float object that contains the same double value as this object. It returns false if both the objects are not same.
3 min read
Field equals() method in Java with Examples The equals() method of java.lang.reflect.Field is used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the sam
3 min read