4/17/19

Difference between equals() and equal to equal operator in java


Difference between equals() and equal to equal operator in java

*The equals() method is actually meant to compare the content of two objects, and not their location in memory.

'==' operator is used to compare two objects (it will consider both content and location in memory)




Example Code..

public class DifferentEqual {

    public static void main(String[] args) {

        String obj1 = new String("ABC");
        String obj2 = new String("ABC");

        System.out.println("equals() Method->");
        if (obj1.equals(obj2))
            System.out.println("True");
        else
            System.out.println("False");

        System.out.println();

        System.out.println("equal to equal operator->");
        if (obj1==obj2)
            System.out.println("True");
        else
            System.out.println("False");


    }

}

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *