0

For a code excerpt like the following

  int a;
  Integer b = new Integer(1);
  a = b.intValue();

the Java compiler generates an "Unnecessary unboxing" warning.

Is that superfluous, or is there more substance behind it? Other answers (like this and that) seem to hinder towards auto-boxing and auto-unboxing being expensive when used extensively, but of negligible to zero impact when done a few times only.

PNS
  • 19,295
  • 32
  • 96
  • 143
  • The unboxing is not the performance eater. As far as I understand it, the boxing part is the performance eater since every autoboxing creates a new `Integer` object (aside from mabye cached values). – Turing85 Apr 26 '18 at 22:25
  • 6
    And besides, `new Integer(1)` is not auto-boxing. It is manual boxing ... done the wrong way. (It should be `Integer.valueOf(1)` or simply `b = 1`.) – Stephen C Apr 26 '18 at 22:28
  • [What does your profiler tell you?](https://stackoverflow.com/questions/890222/analyzing-code-for-efficiency) –  Apr 26 '18 at 22:42
  • An answer like "it is just an overreaction by the JDK" would be adequate, if that were the case. – PNS Apr 27 '18 at 22:59

0 Answers0