Widening and Boxing Java primitives

前端 未结 5 643
一生所求
一生所求 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 02:16

    Short answer

    The java language only supports some level of carelessness.

    Longer answer

    I believe that autoboxing was added to support developer carelessness. Specifically in situations like this: "I need an Integer as a parmeter to the method I want to call, but I have an int. Somehow, new Integer(int) never pops into my head. Instead, I'll just send an int and the java compiler will do the new Integer() call for me. Thanks java carelessness support group!"

    The folks designing autoboxing were willing to support 1 level of carelessness (int => Integer and back), but were not willing to support auto casting of smaller primitive types to larger primitive types in conjunction with automatic creation and extration from primitive type wrapper classes. I suspect the descision matrix for this would be somewhat larger than the decision matrix for the current autoboxing scheme.

提交回复
热议问题