3

I've learned that floating point number s have a signed zero in java. But I'm afraid Integer has not:

new Integer("0").equals(new Integer("-0")) // true

vs.

new Double("0").equals(new Double("-0")) // false

How could I store a sign with my zero Integer value?

Sebastian
  • 5,721
  • 3
  • 43
  • 69

1 Answers1

6

You cannot store a sign with Java integer primitive type.

Negative zero is an artifact of IEEE-754 representation, which stores a sign in a separate bit. Integers, on the other hand, are stored in two's complement representation, which has a unique representation for zero.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you for illuminating :-) for the sake of completeness I'll leave another link here: https://en.wikipedia.org/wiki/Two's_complement – Sebastian Jan 27 '17 at 12:30