aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals method explanation needed regarding this question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals method explanation needed regarding this question" Watch "equals method explanation needed regarding this question" New topic
Author

equals method explanation needed regarding this question

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72

Why line 1 would return false?
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

in line 1, how boolean can be cast to Object Class.


http://muhammadkhojaye.blogspot.com/
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Apr 12, 2009
Posts: 449

make your equals function public instead of private.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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.'


SCJP 88% | SCWCD 84%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Feb 04, 2009
Posts: 40
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.

SCJP 1.6
SCWCD 1.5 (Preparing...)
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Aug 23, 2007
Posts: 124
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

Joined: Apr 12, 2009
Posts: 449

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

Joined: Jul 15, 2009
Posts: 537

String class has its own overrriden equals method.
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Thanks to all..at last i really understand the whole concept of equal method..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals method explanation needed regarding this question
 
Similar Threads
problems from examlab
private variable scope
Wrong Answer in ExamLab SCJP 1.5
Understanding hashcode()
Using PipedInputStream and PipedOutputStream with Threads