Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

contract for equals and hashcode

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to this forum.......& this is my first post.........

Please describe me the reason why the option 3 & 4 are correct?


class SortOf {
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length() * bal);
}
public boolean equals(Object o) {
// insert code here
}
}

Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all that apply.) 
1. return ((SortOf)o).bal == this.bal;
 2. return ((SortOf)o).code.length() == this.code.length();
 3. return ((SortOf)o).code.length() * ((SortOf)o).bal == this.code.length() * this.bal;

 4. return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate == this.code.length() * this.bal * this.rate;
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nisha! Welcome to javaranch.

here we have a rule that if you post any question, then you have to QuoteYourSources. So please tell us from where did you copy this question...
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please use code buttons..........
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is flawed, because none of the specified answers actually fulfill the equals() contract properly, as the equals() contract states:


For any non-null reference value x, x.equals(null) should return false.


This requirement clearly does not hold for any of the answers, as they will in fact throw a NullPointerException in such a case.
 
Nisha Jasiwal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit

I got these lines of code from a exam simulator LearnKey which I got along with SCJP1.5 of kaithy sierra's book.


Ankit Garg wrote:Hi Nisha! Welcome to javaranch.

here we have a rule that if you post any question, then you have to QuoteYourSources. So please tell us from where did you copy this question...

 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nisha Jasiwal wrote:Hi Ankit
I got these lines of code from a exam simulator LearnKey which I got along with SCJP1.5 of kaithy sierra's book.


Now that you have understood how to use the quote button, there is a code button just to the right of the quote button.
So why don't you edit your 1st post to use code buttons, so that it becomes more readable...........
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is an error in the master exam and the correct answer is only 3...
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if null is passed to the equals method???
i mean Object o= null
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kedar Nath wrote:What if null is passed to the equals method???
i mean Object o= null



It depends on the equals method implementation. The equals method of Object class will return false in that case. I hope you are talking about this

nonNullReference.equals(null); //false

but if you call this

Object o = null;
o.equals(anyObject);//NullPointerException
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in the above question all of them should result to such a case ( NUll pointer), if a null is passed.
So none of them are right. Isn't it/ :?:
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kedar Nath wrote:So in the above question all of them should result to such a case ( NUll pointer), if a null is passed.
So none of them are right. Isn't it/ :?:



How can you say none of them is right. It is true that since there is no check so if you pass null into the equals method, then it will result in a NullPointerException. But this doesn't mean that the option is wrong...
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we cant say that the option are right.because question says

Which of the following will fulfill the equals() and hashCode() contracts for this class?



 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the hashCode and equals contract is fulfilled in this case. If you read the documentation, then you will find that it doesn't say anything about null references passed to equals. The contract is for non-null references. Also if you pass a null reference to the equals method, then no doubt it will throw a NPE, but the hashCode method for that null reference will also throw NPE. So the behavior is same for both of them...
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:Well the hashCode and equals contract is fulfilled in this case. If you read the documentation, then you will find that it doesn't say anything about null references passed to equals. The contract is for non-null references. Also if you pass a null reference to the equals method, then no doubt it will throw a NPE, but the hashCode method for that null reference will also throw NPE. So the behavior is same for both of them...



The constract does in fact explicitly mention how null references should be handled.
Take a look at the API documentation for java.lang.Object#equals(Object).
The last bullet says:


For any non-null reference value x, x.equals(null) should return false.


This requirement is violated by all the provided solutions.
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes! I missed that. But I don't think that this was in the mind of the authors...
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:Oh yes! I missed that. But I don't think that this was in the mind of the authors...



I definitely agree, but exam questions really shouldn't be ambiguous
So either the question should prashed more carefully, or the provided answers should be altered to comply with the full contract of the equals() method.
I'd personally prefer the latter.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic