Java: why == works for 127 but not for 1000;
Hello, I am CS student in Thessaloniki, learning Java for my exam. I found something strange and my professor only said "look it up" xD Integer a = 127; Integer b = 127; System.out.println(a == b); // true Integer c = 1000; Integer d = 1000; System.out.println(c == d); // false ;; Why the same code gives different result; Is it a bug in my JDK (Temurin 21); Should I always use equals for Integer; Thank you!
- Can confirm interview question. Also nasty in real life: two Long IDs from Hibernate compared with == in a service. Worked in testing because IDs were small, failed in production once the IDs went past 127. True story from a bank project.