Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - Pointing out the correct statements (Q.No. 3)
3.
Which of the following statements about the hashcode() method are incorrect?
  1. The value returned by hashcode() is used in some collection classes to help locate objects.
  2. The hashcode() method is required to return a positive int value.
  3. The hashcode() method in the String class is the one inherited from Object.
  4. Two new empty String objects will produce identical hashcodes.
1 and 2
2 and 3
3 and 4
1 and 4
Answer: Option
Explanation:

(2) is an incorrect statement because there is no such requirement.

(3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.

Discussion:
6 comments Page 1 of 1.

Sat said:   9 years ago
The hashCode always return positive int value, not a negative value, or float or double etc then how the #2 is wrong?
As #3 as for giving string class inherit hashCode() from object class that is correct because it is mentioned to inheritance not for how the hashCode is calculated for a string?

HEPBO3AH said:   9 years ago
@Jjj you are correct.

Two empty strings do produce identical hashCode(). Also, some Collections DO use hashCode() to store help locate the data, namely HashMap. Its even has a "hash" in its name. Terrible question

Ford Prefect said:   9 years ago
The question is asked in a confusing way. The text should be changed from "incorrect" to "correct" and the answer should be inverted. Also, it's hashCode () not hashcode ().

Jjj said:   10 years ago
Two new empty String objects will produce identical hash codes.

The above will produce unique hash codes right and therefore its incorrect or am I wrong?

Radistao said:   10 years ago
Damn, guys, hashCode(), not hashcode() - JAVA METHODS ARE CASE SENSITIVE!

Avinash said:   9 years ago
Every object uses hashCode, why only for some collections?

Post your comments here:

Your comments will be displayed after verification.