Search code examples
javaoptimizationjit

Java JIT and boxing


I came across code written by a colleague:

return new Integer(_hash).compareTo(other.getHash());

and I am wondering if Java can optimize this avoid the boxing and simply compare the two integers. Does it do that (in Java 7 or 8), or does it continue creating an Integer and calling compareTo() each time?


Solution

  • In Java 7 and 8, surely you should just use Integer.compare. Then you can totally avoid any boxing whatsoever. Simple!