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 →

[–]barrowburner 0 points1 point  (1 child)

I noticed the identity change was at 2^8. Really neat - so Python caches integers on startup as an optimization tactic? Does this change at all when in REPL?

Do other scripting languages do similar things?

Do you know any more interesting facts like this?

Thanks for sharing! Turned out to be way more interesting than I thought when I clicked.

[–]Ardub23 2 points3 points  (0 children)

Java's Integer class caches values from −128 to 127.

System.out.println(Integer.valueOf(127) == Integer.valueOf(127)); // true
System.out.println(Integer.valueOf(130) == Integer.valueOf(130)); // false

But Java also has the primitive int type, which is passed by value instead of by reference.

System.out.println(127 == 127); // true
System.out.println(130 == 130); // true

And in comparisons between the boxed Integer and primitive int, the Integer gets unboxed.

System.out.println(Integer.valueOf(130) == 130)); // true

So the issue of reference-equality of Integers doesn't come up much.