• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 185
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In simple terms, == is used for comparing value types, while .equals is used for reference types.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80616
468
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    Bartender
    Posts: 15741
    368
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the explanations. Especially Joe Bishara. That really helped to clarify things for me.
     
    Greenhorn
    Posts: 14
    Python Angular Framework Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 80616
    468
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic