Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java - Difference between == and .equals()


"equals" is the method of Object class which is supposed to be overridden to check object equality, whereas "==" evaluate to see if the object handlers on the left and right are pointing to the same object in memory.

x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object.


String str1 = new String("String1");

String str2 = new String("String1");

System.out.println(str1 == str2); // prints false

System.out.println(str1.equals(str2));// prints true

str1 = str2; // Now str1 holding the same object as str2

System.out.println(str1 == str2); // prints true





This post first appeared on Techsharepoint, please read the originial post: here

Share the post

Java - Difference between == and .equals()

×

Subscribe to Techsharepoint

Get updates delivered right to your inbox!

Thank you for your subscription

×