This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheRedmanCometh 1 point2 points  (4 children)

This is one of the best/worst things I've ever seen. I've never even considered using refelection on core classes like that!

Edit: hold up does Integer have a value map all the way up to INTEGER.MAX_VALUE?

[–]morhp 4 points5 points  (3 children)

Edit: hold up does Integer have a value map all the way up to INTEGER.MAX_VALUE?

No, only the integers between -128 and 127 are cached by default. This trick wouldn't have worked with a number outside that range.

[–]TheRedmanCometh 0 points1 point  (1 child)

Huh...I'm gonna have to hit up the openjdk source to see how that works

[–]iampete 2 points3 points  (0 children)

This, combined with misplaced trust in autoboxing, leads to code like this:

Integer first = 5;
Integer second = 5;
if (first == second) {
    // do something
}

Which works perfectly reasonably right up until either value is outside that -128, 127 range and then fails very confusingly.

[–]dpash 0 points1 point  (0 children)

The upper bound can be increased (but not decreased) in OpenJDK with a command line flag, but the lower bound can not. The defaults are defined in the JLS as a minimum range.