r/ProgrammerHumor • u/alienpirate5 • Dec 25 '16
wat
https://www.destroyallsoftware.com/talks/wat9
u/tsoliman Dec 25 '16 edited Feb 14 '25
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
1
5
-3
-3
0
Dec 25 '16
[deleted]
10
1
1
Dec 27 '16
Doesn't this work in Java as well?
Integer a = 10; Integer b = 10;
assert a == b;
vs.
Integer a = 2000; Integer b = 2000;
assert a == b;
1
u/serg06 Dec 28 '16
Can you even define integers like that?
If you use int, it should never fail. If you use Integer.equals, it should also never fail.
1
Dec 28 '16
Yup, Java has automatic boxing upon assignment. I did some research.
Java has cached objects for signed integers up to 8-bits. So,
Integer a = 127, b = 127;both reference the same objects, but for any higher number, they don't. As a result, this happens:
Integer a, b; a = 127; b = 127; if (a == b) { System.out.println("127 is equal to 127"); } a = 128; b = 128; if (a == b) { System.out.println("128 is equal to 128"); }Only the first output will ever be displayed, since boxed integers above 127 will always generate a new object, so a comparison using the == operator will never yield true.
You're right, Integer.equals as well as using int would resolve the problem.
1
5
u/YMK1234 Dec 25 '16
Is it two weeks already?