I saw something unpleasant when I woke up. https://twitter.com/EclipseJavaIDE/status/1043084678339719168
Set<Short> set = new HashSet<>();
for (short i = 0; i < 10; i++) {
set.add(i);
set.remove(i - 1);
}
System.out.println(set.size());
Choose your choice.
The reason is that i-1 will change from int type to Integer type, so it will not match the elements of Set held in Short type. .. ..
System.out.println(1 == new Integer(1));
System.out.println(1 == Integer.valueOf(1));
System.out.println(new Integer(1) == new Integer(1));
System.out.println(Integer.valueOf(1) == Integer.valueOf(1));
System.out.println(new Integer(1) == Integer.valueOf(1));
The result is as follows. true true false true false
System.out.println(Integer.valueOf(-129) == Integer.valueOf(-129));
System.out.println(Integer.valueOf(-128) == Integer.valueOf(-128));
System.out.println(Integer.valueOf(127) == Integer.valueOf(127));
System.out.println(Integer.valueOf(128) == Integer.valueOf(128));
The result is as follows. false true true false
It's for efficiency. The 127 and 128 thresholds should have changed in the system properties.
System.out.println(1 + 1 == new Integer(1) + 1);
System.out.println(new Integer(1 + 1) == new Integer(1) + 1);
The result is as follows. true true
The right side will be an int type.
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(128);
list.add(128);
System.out.println(list.get(0) == list.get(1));
System.out.println(list.get(0) == list.get(1) + 0);
System.out.println(list.get(2) == list.get(3));
System.out.println(list.get(2) == list.get(3) + 0);
The result is as follows. true true false true
If you take +0, it's nice that the result changes depending on the range of values.
Probably it doesn't work well because I understand the specifications and it doesn't +0, but it should be like this. In addition, I don't know what it is, but I said that I needed +0, and even if I did a regression test, I didn't notice that the behavior changed when I did it in about 1, 2 or 3, and the bomb was set. ..
Ahaha.