aspose file tools*
The moose likes Beginning Java and the fly likes some  prob  with ...comparing object.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "some  prob  with ...comparing object.." Watch "some  prob  with ...comparing object.." New topic
Author

some prob with ...comparing object..

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vyas
Greenhorn

Joined: May 21, 2007
Posts: 17
I thought equalsIgnoreCase() compares the case
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: some prob with ...comparing object..