1

How / why is is that autoboxing works for the first example below, but not the second? What's the reasoning / logic behind this?

Short i = 5; // works
Short i = new Short(5) // doesn't work

Short i = new Short( (short) 5) // works again, but uses explicit cast
Rahul
  • 44,383
  • 11
  • 84
  • 103
abc32112
  • 2,457
  • 9
  • 37
  • 53

1 Answers1

3

As the javadoc indicates, there is no constructor in Short taking an int as argument. And 5 is an int.

Autoboxing is irrelevant. What would be needed for this to work is auto-narrowing.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255