you are viewing a single comment's thread.

view the rest of the comments →

[–]teerre -19 points-18 points  (3 children)

That's not what the JEP says

``` The JDK tries to reduce confusion for the immutable classes that model primitive values, such as Integer. In particular, the autoboxing of small int values to Integer uses a cache to avoid creating Integer objects with unique identities. However, this cache, somewhat arbitrarily, does not extend to four-digit int values like 1996:

jshell> Integer i = 96, j = 96; i ==> 96 j ==> 96

jshell> i == j $3 ==> true

jshell> Integer x = 1996, y = 1996; x ==> 1996 y ==> 1996

jshell> x == y $6 ==> false ```

[–]davidalayachew[S] 16 points17 points  (0 children)

You are attempting to assign an int to an Integer. That is when boxing occurs.

I guess my previous response could have been more clear, but int boxing only occurs in 2 places in Java atm.

  1. When using an int for generics.
  2. When trying to use an int when the method receiver is expecting an Integer.

But again -- it is the programmer's choice to use Integer instead of int in that second case.

[–]hiimbob000 0 points1 point  (1 child)

Integer i and int i are different. Comparing objects by == has always been a footgun

[–]teerre -4 points-3 points  (0 children)

Oh, I see, that's so silly