Recent Tutorials and Articles
    == operator vs equals() method
    Published on: 2018-05-25 16:05:17

    This tutorial describes the difference between the double equal to (==) operator and equals() method.

    Before we jump to comparison of the == operator and equals() method. Let's spend some time understanding how various data types are managed in memory in Java.

    In Java, data types are broadly categorized as -

    1. Primitive data types - These are total 8 in number as follows - byte, short, char, int, long, float, double, boolean.
    2. Reference data types - Objects - All the Objects fall under the reference data types.

    Let's consider following code snippet

    int i = 0;
    Object object = new Object();
    
    

    We see that there are two variables - i (of primitive type - int) and object (of reference type - Object).

    In first case, variable i is directly storing the value 0 while in the second one, variable object is just storing the reference (memory address) of the Object instead of storing Object directly as depicted below.

    Sorry, image couldn't be loaded

    Since we have a basic understanding of how the data is stored in memory, let's get back to == and equals() comparison. For primitive types, this comparison isn't applicable as you can't call equals() methods (or any method for that matter) on these.

    The operator, ==, checks whether 2 reference variables being compared are referencing the same instance of a class. This is also referred as identity operator and hence will always return false if 2 variables are referencing to different objects. On the other hand, equals() method compares the objects' states (fields) rather than objects' references and therefore equals would return true if both objects are same or the fields compared of the objects are same.

    DoubleEqVsEquals.java
    /**
    * 
    */
    package com.sts.allprogrammingtutorials.javabasics;
    
    /**
    * @author Sain Technology Solutions
    *
    */
    public class DoubleEqVsEquals {
    
    	/**
    	* @param args
    	*/
    	public static void main(String[] args) {
    		// Case 1 - Reference variables referencing to same Object
    		System.out.println("Case 1 - Reference variables referencing to same Object");
    		String str = new String("STS");
    		String case1str1 = str;
    		String case1str2 = str;
    		System.out.println("Case1: ( case1str1 == case1str2 ): " + (case1str1 == case1str2));
    		System.out.println("Case1: ( case1str1.equals(case1str2) ): " + case1str1.equals(case1str2));
    
    		//Case 2 - Reference variables referencing to different objects with same data (state)
    		System.out.println("\nCase 2 - Reference variables referencing to different objects with same data (state)");
    		String case2str1 = new String("STS");
    		String case2str2 = new String("STS");
    		System.out.println("Case2: ( case2str1 == case2str2 ): " + (case2str1 == case2str2));
    		System.out.println("Case2: ( case2str1.equals(case2str2) ): " + case2str1.equals(case2str2));
    
    
    		//Case 3 - Reference variables referencing to different objects with different data (state)
    		System.out.println("\nCase 3 - Reference variables referencing to different objects with different data (state)");
    		String case3str1 = new String("STS1");
    		String case3str2 = new String("STS2");
    		System.out.println("Case3: ( case3str1 == case3str2 ): " + (case3str1 == case3str2));
    		System.out.println("Case3: ( case3str1.equals(case3str2) ): " + case3str1.equals(case3str2));
    	}	
    }
    
    

    Here is the output that you will see after running the program -

    Output:
    Case 1 - Reference variables referencing to same Object
    Case1: ( case1str1 == case1str2 ): true
    Case1: ( case1str1.equals(case1str2) ): true
    
    Case 2 - Reference variables referencing to different objects with same data (state)
    Case2: ( case2str1 == case2str2 ): false
    Case2: ( case2str1.equals(case2str2) ): true
    
    Case 3 - Reference variables referencing to different objects with different data (state)
    Case3: ( case3str1 == case3str2 ): false
    Case3: ( case3str1.equals(case3str2) ): false
    
    

    We have covered 3 scenario in the above code.

    1. In first one, both variables are referencing to same instance hence both == operator and equals result into true.
    2. In second one, since referencing variables are referencing to different objects, operator == returns false but since String object data ("STS") is same, equals return false.
    3. In last scenario, both referencing variables are referencing to different objects with different value, both == and equals return false.

     

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 2018-05-25 16:05:17

    Comment Form is loading comments...