*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equlas() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equlas()" Watch "equlas()" New topic
Author

equlas()

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477



i got the lin1 execution.i have a doubt in line2.

1.ob2.equlas() means deep comparision.but here ob2 is obj2 object and having no value.and oba is obj1 objeact and having values (1,2).how come the result true is displaying.

2.How come the lin3 is not showing any classcastException

Thanks in advance.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
>>1.ob2.equlas() means deep comparision.but here ob2 is obj2 object and having no value.and oba is obj1 objeact and having values (1,2).how come the result true is displaying.

..is because, you are initializing it to 1 and 2 in the class declaration of obj2. Try changing the defaults to different values in obj2 and see what happens.


>>2.How come the lin3 is not showing any classcastException
Why? In the class obj2, as per your code, at line 3, it is expecting an object of type obj1 and in the main() you are passing obj1, therefore no exception.
At line 2, try passing ob2 instead
and you will see the exception.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In the obj2 class, you have two instance variables that are initialized when an object of type obj2 is created.

In this case you initialize them to 1 and 2.

These are the same values of the variables in the obj1 object you send.

The reason there isn't a ClassCastException is that you are converting the reference to type obj1, and you are send a reference of type obj1 to the equals method.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equlas()
 
Similar Threads
Collections
equals() and hascode() doubt
Question on Object and equals
hashCode contract
Doubt in equals()