Forums Register Login

equals method explanation needed regarding this question

+Pie Number of slices to send: Send

Why line 1 would return false?
+Pie Number of slices to send: Send
in line 1, how boolean can be cast to Object Class.
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
make your equals function public instead of private.
+Pie Number of slices to send: Send
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.'
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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...

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
String class has its own overrriden equals method.
+Pie Number of slices to send: Send
Thanks to all..at last i really understand the whole concept of equal method..

So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1005 times.
Similar Threads
problems from examlab
private variable scope
Understanding hashcode()
Wrong Answer in ExamLab SCJP 1.5
Using PipedInputStream and PipedOutputStream with Threads
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:26:52.