Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

some prob with ...comparing object..

 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it has been disscussed by some other users to....but my prob is...

#Ex1.
Pizza pizza1=new Pizza("Veggedelight");
Pizza pizza2=new Pizza("Veggedelight");
Pizza pizza3=new Pizza("Chesedelight");
boolean test1=pizza1.equals(pizza2); //false ....(1)
pizza1=pizza2;
boolean test2=pizza1.equals(pizza2); //true .............(2)

#Ex2.
String Movie1 =new String("Harry Potter");
String Movie2 = new String("Harry Potter");
boolean test3=Movie1.equals(Movie2); ...//true (3)

we know that == operator compares the refrences of the object whereas Object.equals() method compares the content i.e their state.

now how is that possible that line 1 results false and line 3 comes to be true ,When in both the cases the objects has same content
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we know that == operator compares the refrences of the object whereas Object.equals() method compares the content i.e their state.


The == operator does compares the references of the objects -- but so does the Object.equals() method. To get the equals method to compare the "state", you need to override the method.

Henry
 
Vyas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought equalsIgnoreCase() compares the case
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vyas:
I thought equalsIgnoreCase() compares the case


The Object class does not have an equalsIgnoreCase() method. However, the String class does. The String class also overrides the equals() methods so that it compares the values of the strings -- instead of just references.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic