• Post Reply Bookmark Topic Watch Topic
  • New Topic

.equals() vs == What is the difference?  RSS feed

 
Joseph Williamson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been using the "==" operator in most of my programs to compare my integers, but looking on the internet I came across ".equals()". Is "==" bad? When should I use "==" and when should I use ".equals()"? What's the difference?
 
Salil Wadnerkar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In simple terms, == is used for comparing value types, while .equals is used for reference types.
 
anotida Maditsha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The “==” operator

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory.
A very simple example will help clarify this:


String obj1 = new String("xyz");

String obj2 = new String("xyz");

if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");


The code above will actually output:

obj1==obj2 is FALSE


Equals() method
It checks only the values of the strings, not their locations in memory. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal. Here is an example that will help clarify this:

String obj1 = new String("xyz");

String obj2 = new String("xyz");

if(obj1.equals(obj2))
System.out.printlln("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");


This code will output the following:

obj1==obj2 is TRUE
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph Williamson wrote:What's the difference?

Difference 1
== can be used to compare a primitive to another primitive, however, equals() cannot be used to compare a primitive to another primitive


Difference 2
== and equals() can both be used to compare objects, however, == never does a thorough equality check i.e. it returns true only if the two object references you’re comparing refer to the same object. By default, equals() simply calls == i.e. it also doesn’t do a thorough equality check, however, you can override it to do a thorough equality check, for example, Integer overrides equals() to return true if the two object references you’re comparing refer to Integer objects that contain the same value.


Note that this code produces a different result due to autoboxing i.e. x and y refer to the same Integer object

Difference 3
When comparing objects, == causes a compiler error if the two object references you’re comparing are not related. equals() doesn’t cause a compiler error if the two object references you’re comparing are not related, however, it should return false, for example, comparing an Integer to a Long always returns false even though they have the same value. This is because Integer and Long are not related i.e. Long is not in the Integer inheritance hierarchy and Integer is not in the Long inheritance hierarchy. The Long inheritance hierarchy is composed of Long, Number, Serializable, Comparable and Object.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== is sort of like asking "do these two slips of paper have the same bank account number written on them?".

.equals() is sort of like saying "do these two bank accounts have the same balance?". Note that the two bank accounts may actually be the same bank account, in which case the answer is "yes" without even looking at what the balance is.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salil Wadnerkar wrote:In simple terms, == is used for comparing value types, while .equals is used for reference types.
Afraid that is incorrect. A value type is a reference type. Read this FAQ for more details.
You use == for all sorts of primitives representing numbers, or for boolean variables (not Boolean). Never use == true or == false.
You should use == on reference types under the following four circumstances:-
  • 1: As the first line of an equals method.
  • 2: When either operand is the reserved word null.
  • 3: When either operand is an enum constant.
  • 4: When you are simply trying it out to see what happens. Obviously not for real‑life code.
  •  
    Stephan van Hulst
    Saloon Keeper
    Posts: 7817
    142
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Salil Wadnerkar wrote:In simple terms, == is used for comparing value types, while .equals is used for reference types.
    Afraid that is incorrect. A value type is a reference type. Read this FAQ for more details.

    I'm not sure where it says that. In my understanding, Salil is correct. Value types generally refer to types where the assignment operator performs a deep copy. In Java, those are only the primitive types.
     
    Joseph Williamson
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the explanations. Especially Joe Bishara. That really helped to clarify things for me.
     
    Harsha kumar Reddy
    Greenhorn
    Posts: 12
    Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Object.equals() this method is available to every object since it is method of Super class of every class in java .
    In Object class this method is defined to compare the object reference and that is what the == operator also does it compares the object reference .So it make no difference between == and .equals() method when you don't override the .equals() method.

    It is programmers responsibility to override the equals() method so that it compares the values in the object with another and evaluates as you see it right.
     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:. . . In my understanding, Salil is correct. . . .
    I obviously misinterpreted that as value objects; sorry for my mistake.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!