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

前端 未结 5 746
余生分开走
余生分开走 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:41

    Basically what this means is that widening only works with primitive types, not the wrappers. If you box first, you get a wrapper (byte -> Byte). But the wrappers - Byte, Double, Float, Integer, Long and Short do not have any inheritance relationship (IS-A). A Long parameter can't accept a Byte for instance.

    So you must widen first (byte -> long) and then box (long -> Long).

提交回复
热议问题