Why widening beats both Boxing and var-args in Overloading of a method?

前端 未结 7 2296
不知归路
不知归路 2020-12-06 13:03

I am preparing for a SCJP exam and when studying widening part it\'s given that widening beats both Boxing and Var-args in overloading but there is no clear explanation. Tri

7条回答
  •  爱一瞬间的悲伤
    2020-12-06 13:37

    I don't know about you, but I'd much rather that the compiler passed my byte as an int than as a Byte. Consider the overhead. And varargs also requires boxing.

    In other words, the reason is efficiency. The language design prefers the more efficient calling mechanism that does not require it to allocated a boxed item.

    'Requires', you ask? The varargs functions expect to get an array of Object, and that can't include a primitive type.

    Compatibility is not a bad reason, either.

提交回复
热议问题