13
    System.out.println((byte) (1.0/0));
    System.out.println((short) (1.0/0));
    System.out.println((int) (1.0/0));
    System.out.println((long) (1.0/0));

The result is:

    -1
    -1
    2147483647
    9223372036854775807

In binary format:

    1111 1111
    1111 1111 1111 1111
    0111 1111 1111 1111 1111 1111 1111 1111
    0111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111

Why casting infinity to int and long integers keeps sign bit as "0", while sets sign bit to "1" for byte and short integers?

  • 3
    I assume it is casting to an `int` and then to a `byte` Note: Integer.MAX_VALUE is the closest value to Infinity for an `int`. – Peter Lawrey Mar 25 '16 at 20:50
  • Thx Peter, but casting Integer.MAX_VALUE to 'long' would give us just '2147483647L', am I right? – Alexander Radchenko Mar 25 '16 at 20:53
  • @PeterLawrey Wouldn't it be possible for `(1.0/0)` to be evaluated as a `double`, and then the caste to byte consider the bottom 8 bits? While IEEE 754 defines the standard for `double` and `float`, I find it odd that the sign bit is 0 for `int` since the docs don't define infinity specs for `int`. – Debosmit Ray Mar 25 '16 at 20:55
  • 1
    You don't cast `Integer.MAX_VALUE` to `long`. If the type is `long`, it chooses the maximum representable value in case of +Infinity which is `Long.MAX_VALUE`. – Alexis C. Mar 25 '16 at 20:59
  • Possible duplicate of [How does double to int cast work in Java](http://stackoverflow.com/questions/12514958/how-does-double-to-int-cast-work-in-java) – Dragan Bozanovic Mar 25 '16 at 21:01
  • 1
    @DebosmitRay the top bit is 0 for `int` the maximum representable value and when this is cast to short or byte, the top bit is `1`. – Peter Lawrey Mar 25 '16 at 21:28
  • @Dragan Bozanovic - should I press button "That solved my problem" about the link you supplied? That Q is similar, but I couldn't find it more than hour searching... Thx everyone, answered my Q very fast. – Alexander Radchenko Mar 25 '16 at 21:31
  • @AlexanderRadchenko It's up to you. You don't have to if you think that your question is not an exact duplicate of that one. – Dragan Bozanovic Mar 25 '16 at 21:42

1 Answers1

10

JLS 5.1.3:

A narrowing conversion of a floating-point number to an integral type T takes two steps:

In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.

Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.

Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

Otherwise, one of the following two cases must be true:

The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

In the second step:

If T is int or long, the result of the conversion is the result of the first step.

If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.

So an infinite double value is first cast to int by returning Integer.MAX_VALUE, and then it is further cast to byte/short, which takes the appropriate number of low bytes (and gets -1 as a result). Casts to int and long don't have that extra step, but byte and short go first through int and then to byte/short.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413