Consider 2 loops with Integer::<init> & Integer::valueOf:
@Benchmark
public void testIntegerValueOf(Blackhole bh) {
int sum = 0;
for (int idx = 0; idx < data.length; idx++) {
sum += Integer.valueOf(data[idx]).intValue();
}
bh.consume(sum);
}
@Benchmark
public void testIntegerNew(Blackhole bh) {
int sum = 0;
for (int idx = 0; idx < data.length; idx++) {
sum += new Integer(data[idx]).intValue();
}
bh.consume(sum);
}
$ java -jar bench_autobox.jar -jvmArgs '-XX:+UnlockExperimentalVMOptions -XX:+AggressiveUnboxing' '.*'
IteratorBench.testIntegerNew 3.130 �� 0.053 us/op
IteratorBench.testIntegerValueOf 12.546 �� 0.247 us/op
The problem is that VM doesn't unroll loop in testIntegerValueOf because it's not detected as counted.