LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Find the Error · Question 8

Error: hashCode() declared to return long Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links