Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Equality (==) operator in Java with Examples
Next article icon

Equality (==) operator in Java with Examples

Last Updated : 24 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

== 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));     } } 

Output
Are 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));     } } 

Output
Are a and b equal? false Are b and c equal? true

Next Article
Equality (==) operator in Java with Examples

S

ShivendraPorwal
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Operators
Practice Tags :
  • Java
  • Java-Operators

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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences