• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals method explanation needed regarding this question

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why line 1 would return false?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in line 1, how boolean can be cast to Object Class.
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Muhammad, Actually i couldn't understand your tricky answer, can you please explain this, as i think "pf1.equals(pf2) returns false and is assigned to "Object o", but why equals method of Object class return false? explain this part, as it has made my concept mess up..
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make your equals function public instead of private.
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You realize that Profile's equals() method is not running--Object's equals() method is running and so, because the two Profile objects
are not the same, false is returned.

You could not legally call Profile's equals() method from another class because it is private. What is curious here is that the jvm chooses
Object's equals method when it sees equals(Profile p) called. It's choices are:

public boolean equals(Object o)
-and-
private Boolean equals(Profile p)

If private Boolean equals(Profile p) had been unambiguous, it would have been an error at compile time to call it from a reference to Profile
object within another class. It seems like you're getting off easy here, as though the compiler/jvm is saying 'we'll overlook this illegal call
to equals(Profile p) because this call could also apply to equals(Object o) which is legal.'
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh you don't need to write the code tags yourself by hand. You are writing [Code] instead of [code]. There is a button to insert code tags in your posts...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol Rajesh you are asking all examlab questions here..... well Let me tell that this is not a valid override but a overload. Remember contract of a valid override. Access modifiers cannot be restrictive and for overloading the arguments must change so this equals is a overload. Well because its a overload and not a override, the line 1 uses the default equals method(also the class's equals method is not visible to the other class because its private) that is inherited from the Object class thus giving you false. Well in Object class equals method is implemented as a simple (==).

Cheers.
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really very thankful to all the Ranch, but still i have one thing unclear..

if i use

It gives "True"

but when i use a class as it is given in my question


I know this is a bit silly question..but still i want to learn why i m unable to understand the concept...

 
Samrat Som
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s1.equals(s2) returns true , due to string Constant Pool Initialization. When there is already a " ab " in the heap . Then the next reference though it is a instantiation by new operator. But it points to the same reference in the heap. This is something exceptional in the case of String. I hope this will help you.
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Samrat, you are quite right? but my question is about the code give below



Why 1 results in true and 2 results in false?
Can you please help me in this.
Or anyone can please help me in this.
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String class overloads equals() to return true if the 'String' stored by the String objects are the same. Your Profile
class, given that the overloaded equals() method is private, still basically uses Object's equals() method which returns
false if the references do not point to the exact same object.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samrat Som wrote:s1.equals(s2) returns true , due to string Constant Pool Initialization. When there is already a " ab " in the heap . Then the next reference though it is a instantiation by new operator. But it points to the same reference in the heap. This is something exceptional in the case of String. I hope this will help you.



When you initialize String object using new operator, then there is no concept of Pool occur. All objects have different memory in Heap.

Rajesh k Jha wrote:Why 1 results in true and 2 results in false?
Can you please help me in this.


The String class has its own implementation of Equal function which compare the values instead of refrences that's why it returns true.

 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String class has its own overrriden equals method.
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all..at last i really understand the whole concept of equal method..

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic