File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals and == with Objects Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Cannot understand the above code



As I Know,
o2 refers to String with value false.since ,new operator is used there would 2 strings one, stored on heap and other in string literal pool
o4 refers to string false by

so,it would refer to string in literal pool.
Thus,o4 should be equal to o3

Since equals of object class would be used as o2 is of type Object,equals would use == for comparison(Equals of object class would be used)
Then,


if o2.equals(o4) returns true.Then,how is o2==o4 false,???


DOES equals of object called would be used or of Boolean or String ??
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if I understood you completely.

mohitkumar gupta wrote:
Thus o4 should be equal to o3


Of what type is o3? What are you comparing it with?


if o2.equals(o4) returns true.Then,how is o2==o4 false,???


Because, as you explained yourself, you have different String objects that are equal but not identical.

Due to polymorphism the method from the current object gets called. If you have a String reference, then String's equals method would be called.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even I am not sure whether I understood your question or not
but I will try to explain the code line by line
here it is


hope this makes sense
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic