What is the exact Difference between .equals() and == with an Example?.

What is the exact Difference between .equals() and == with an Example?.

Here we can get the difference between “==” operator and “.equals()” method. the most of the interview persons expect online answer–> you can tell like this.

  1.  “==” operator meant for “Reference Comparison/ Address Comparison
  2.  “.equals()” method meant for Content Comparison.
Example For Reference Comparison and Content Comparison:

Eg:  
String s1= new String(“Interview”);
String s2= new String(“Interview”);
 System.out.println(s1==s2);  //line3
 System.out.println(s1.equals(s2));//line4
 what will get the output of line 3 and 4.
  • Here we have s1 and s2 are two different objects and it creates two memory locations. fig:a is the example.
  • s1 points to one location and s2 points two another location then the line 3 output is false.(reference wise s1 and s2 are not Equal)
  • (s1.equals(s2)) is comparing the contents of the object then the output of line 4 is true.even though object are different but content is same.(Content wise both are equal)
Note:if both objects are pointing to same memory address location then the output == is True.

One thought on “What is the exact Difference between .equals() and == with an Example?.

  • July 25, 2016 at 7:37 pm
    Permalink

    Very good article. I was delighted to come across this since I was also conceived in New York State. also, we appreciate your traveling San Francisco Bay Area and helping me to better discover baking skills. This assisted me with my admittance to University of North Carolina. Getting excited about seeing you in our house and catch up with our badminton game.

Leave a Reply