google / google/gson

JsonPrimitive's hashCode() function fails to obey the hashCode contract for some numeric values

Open
#523 0 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

```
JsonPrimitive's hashCode() function sometimes fails to obey the hashCode
contract.

From Object.hashCode()'s Javadoc:
"If two objects are equal according to the equals(Object) method, then calling
the hashCode method on each of the two objects must produce the same integer
result."

With JsonPrimitive, this is not the case for equal double/integer values. For
example, a JsonPrimitive of 1 (integer) equals a JsonPrimitive of 1d (double),
but they produce different hash codes.

Reproducing code:
JsonPrimitive p1 = new JsonPrimitive(1);
JsonPrimitive p2 = new JsonPrimitive(1d);
System.out.println(p1.hashCode()); // prints 1
System.out.println(p2.hashCode()); // prints 1072693248
System.out.println(p1.equals(p2)); // prints true
```

Original issue reported on code.google.com by `bcai...@google.com` on 14 Jun 2013 at 10:26

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.