• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

some prob with ...comparing object..

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought equalsIgnoreCase() compares the case
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic