Hidden Features of Java

This is a nice ‘question’ on StackOverflow: What are hidden features in Java?

I didn’t read about this nice one:


Integer integ1_a = 1;
Integer integ1_b = 1;
Integer integ1_c = new Integer(1);
Integer integ1_d = new Integer(1);

Integer integ128_a = 128;
Integer integ128_b = 128;

assertTrue (integ1_a   == integ1_b);   // again: this is true!
assertFalse(integ128_a == integ128_b); // again: this is false!
assertFalse(integ1_c   == integ1_d);   // again: this is false!

Read more about this issue by searching java’s pool of integer (internal ‘cache’ from -128 to 127 for autoboxing) or look into Integer.valueOf

Of course you can vote for my comment on stack overflow😉