What’s the difference between equals() and ==?

Before discussing the difference between “==” and the equals() method, it’s important to understand that an object has both a location in memory and a specific state depending on the values that are inside the object.

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. In other words, it checks to see if the 2 object names are basically references to the same memory location. 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");
  System.out.println("obj1==obj2 is FALSE");

Take a guess at what the code above will output. Did you guess that it will output obj1==obj2 is TRUE? Well, if you did, then you are actually wrong. Even though the strings have the same exact characters (“xyz”), The code above will actually output:

 obj1==obj2 is FALSE

The “==” operator compares the objects’ location(s) in memory

Are you confused? Well, let us explain further: as we mentioned earlier, the “==” operator is actually checking to see if the string objects (obj1 and obj2) refer to the exact same memory location. In other words, if both obj1 and obj2 are just different names for the same object then the “==” operator will return true when comparing the 2 objects. Another example will help clarify this:

String obj1 = new String("xyz");

// now obj2 and obj1 reference the same place in memory
String obj2 = obj1;

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

Note in the code above that obj2 and obj1 both reference the same place in memory because of this line: “String obj2 = obj1;”. And because the “==” compares the memory reference for each object, it will return true. And, the output of the code above will be:

obj1==obj2 is TRUE

The equals() method

Now that we’ve gone over the “==” operator, let’s discuss the equals() method and how that compares to the “==” operator. The equals method is defined in the Object class, from which every class is either a direct or indirect descendant. By default, the equals() method actually behaves the same as the “==” operator – meaning it checks to see if both objects reference the same place in memory. But, the equals method is actually meant to compare the contents of 2 objects, and not their location in memory.

So, how is that behavior actually accomplished? Simple – the equals class is overridden to get the desired functionality whereby the object contents are compared instead of the object locations. This is the Java best practice for overriding the equals method – you should compare the values inside the object to determine equality. What value you compare is pretty much up to you. This is important to understand – so we will repeat it: by default equals() will behave the same as the “==” operator and compare object locations. But, when overriding the equals() method, you should compare the values of the object instead.

An example of the equals() method being overriden

The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that 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");

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

This code will output the following:

obj1==obj2 is TRUE

As we discussed, Java’s String class overrides the equals() method to compare the characters in a string. This means that the comparison between the 2 String objects returns true since they both hold the string “xyz”. It should now be clear what the difference is between the equals() method and the “==” operator.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

  • Rishav Choudhuri

    Error will come. Because Integer is not a primitive data type.

  • ajay kumar

    good explanation 🙂

    In the last explanation, It is mentioned that, equals() also compares the object’s memory location. is it right?. It actually checks the content in that object.

  • HL

    I tried to run this, and for all values below 128 it gives true, for all above it gives false… How come?

  • HL

    How does this work?

  • rahul dubey

    Very good explanation,

  • navjot

    who is this pappu

  • Gio Walls

    what is the difference between > and /> ?

  • kj

    This was very helpful! Thank you very much. the best place to learn java

  • Preetha

    Thank you….this is clear my comparison and equalsTo methods confusion

  • Sandeep Amarghde

    == compare the memory location that are allocated by the object are same or diff memory space
    like 0bj1==obj2
    == compare hase code of two objects

    equals() compare the contents of two objects
    if contents are same retun true

  • Sandeep Amarghde


  • Priya

    Well i hav a dbt!!!
    What is the output of the following code?
    Integer i1 = 128;

    Integer i2 =128;

    System.out.println(i1 == i2);

    Integer i3 = 127;

    Integer i4 =127;

    System.out.println(i3 == i4);

    Thank yu in advance!!!

  • Suraj Kumar

    plz telll me code of this simple output.,, A=10 And B=10…
    when integer a=5; b=10;

  • Good one 🙂

  • Arun


  • Ankita Monga Sharma

    Awsome piece of text superlike

  • Neha

    Sometimes == gives illusions of comparing String values, in following cases

    String a=”Test”;
    String b=”Test”;
    if(a==b) ===> true

    This is a because when you create any String literal, JVM first searches for that literal in String pool, if it matches, same reference will be given to that new String, because of this we are getting

    (a==b) ===> true

    String Pool
    b —————–> “test” false

  • Gopinath M B

    Please note that for primitive variables we must always use ‘==’ because they are not associated with any class or object. Primitive variables will not have any class to associated with it so no question of any ‘equals()’ method to it.
    int x;
    (x % 100) ===> will be some integer value and hence u cant like if(10.equals(0)) // Compiler error since we can call methods only on objects!!!
    If you really wnat to use equals() method then you should go for wrapper classes i.e for int it is java.lang.Integer class
    I think u got it 🙂

  • venky

    You couldn’t pick more wrong example to explain the difference between ==, and equals().

  • Das

    Awesome explanation. Thanks dude

  • crime_master_gogo

    this is awesome explanation. kudos

  • Salma Hassan

    Thank you very much for this helpful article , but I have a question please :

    I used to use == always like
    if ( x % 100 == 0 )
    so why I didn’t have any errors ?!

    and another question please I still didn’t understand well when I should use == or equal() ?!

    Thank you again!

  • Deepak Rastogi

    mr.artem So what java using string pool so if u have a 2 object then string pooll another using new pool making.
    and let us consider carefully
    if u have first obj1,and second obj2 then the jvm using diffrent diffrent string pool.

    check it now JVM

  • Muhammad Shahrukh Khan


  • Arundev Puthenpurayil

    String s1=new String(“xyz”);
    String s2=new String(“xyz”);

    sop(s1==s2) will print false

    you can make s2==s2 true only if you call the intern() method

    String s1=new String(“xyz”).intern();
    String s2=new String(“xyz”).intern();

    sop(s1==s2) will print true.

  • Abhi

    Yes it is supposed to be false

  • Nikhil

    Great Job done…Thanks a Ton

  • yo sign


  • prasanth

    awesome man

  • Amit

    thanks 🙂 good example..

  • Mariam

    Good explanation…Thanks!

  • bashu

    This is a very good explanation. Thanks Dude

  • Siqi Liu

    do you mean

    S.o.p(s1==s2))->>>>false? “?

  • rahul

    true,because here create only reference of String not provide memory (because memory provided by new operator) so "run time" the s1 will be compare with s2's content and here the content are same so both s1 and s2 refer to same memory address
    String s1=new String("xyz");
    String s2=new String("xyz");

  • Marcelo Aravena

    Hello Artem, this is because Java use an String Pool since you didn't use new, so then Java use this pool and put the literal "xyz" that's why when comparing with == in fact it is the same memory location.
    Please read these examples in this site for a more clear explanation: http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3

  • sac

    thnx varoon18…it helped me a lot

  • james

    <a href=¨http://stringpool.com¨>Nice post Sir, Thanks a lot !!!</a>

  • Artem

    String s1 = "xyz";
    String s2 = "xyz";
    System.out.print(s1==s2) ->>> True

  • bern

    Lets say first input is Hi and 2nd input is also Hi .. what I dont understand is when I input 2 strings and and compare them using result=a.compareTo(b), I dont know if how "if(a<0){print("a is less than b")}" respond to this comparison, and lets say that the output is "a is less than b" .. why is that?

  • Kas

    Have better idea.. thanks

  • varoon10

    Will do that soon, thanks Shady!

  • varoon10

    Thanks Prakash!

  • varoon10

    Thanks Teju!

  • varoon10

    Thanks Anand!

  • varoon10

    Thanks Naidu!

  • Anand

    superb Example

  • k ashok

    helped a lot

  • Teju

    too good

  • Praksh


  • Shady

    this method , please give more examples.

  • S Roopesh

    Thanks a lot

  • Naidu

    Thaks, I got clear idea now.