Widening and Boxing Java primitives

前端 未结 5 649
一生所求
一生所求 2020-12-07 01:33

Widening and Boxing Java primitives.

I know it is not possible to widen a wrapper class from one to another as they are not from the same inheritence tree. Why thou

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 01:57

    Why? Because boxing / autoboxing is only some compiler sugar and not a new type system. It's badly designed and causes trouble at least as often as it simplifies things.

    But here are some workarounds for your compile error:

    goInteger((int) b);
    
    // these are equivalent
    goInteger(((Byte) b).intValue());
    goInteger(Byte.valueOf(b).intValue());
    

提交回复
热议问题