SCJP: can't widen and then box, but you can box and then widen

前端 未结 5 749
余生分开走
余生分开走 2020-12-17 03:05

I\'m studying for the SCJP exam and I ran into an issue I can\'t really wrap my head around.

The book says you can\'t widen and then box, but you can box and then wi

5条回答
  •  别那么骄傲
    2020-12-17 03:37

    In essence the rules stand that you can box then widen but not widen then box. However if boxing then widening then the class being widened must be in the same inheritance tree as the class you are widening to.

    Whilst the example shown is trying to box then widen, the reason it is invalid is because Byte is not a Long, ie they are not in the same inheritance tree. However if the example used Byte and Numeric, then you it would be valid to Box byte to Byte and then widen Byte to Numeric as Byte IS-A Numeric (they ARE in the same inheritance tree)

提交回复
热议问题