Question 8
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
class Order {
final long id;
Order(long id) { this.id = id; }
@Override
public long hashCode() { return id; }
}
public class Main {
public static void main(String[] args) {
System.out.println(new Order(7).hashCode());
}
}
- A id must be int
- B Object.hashCode() returns int; an override cannot change a primitive return type to long – use Long.hashCode(id) Correct
- C @Override is not allowed on hashCode
- D Order must implement Hashable
Correct Answer: Option B
javac: "return type long is not compatible with int".
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!