Var-arg of object arrays vs. object array — trying to understand a SCJP self test question

前端 未结 2 880
面向向阳花
面向向阳花 2021-01-05 23:45

I\'m having trouble understanding this question, and the explanation of the answer for an SCJP 1.6 self test question. Here is the problem:

class A { }
class         


        
2条回答
  •  走了就别回头了
    2021-01-05 23:59

    1. When attempting to determine which method to invoke, the compiler first looks for non vararg method (e.g. sifter(Object)) before considering a vararg one (e.g. sifter(A[]...)), when both of the methods belong to the same class (more or less).

    2. Since an array is an Object, the invocation of sifter(aa) will match sifter(Object), hence not even considering sifter(A[]...).

    3. Starting from Java 5, the compiler may "box" primitive, i.e. convert primitive values (e.g. int) to their corresponding Object (e.g. Integer). So for sifter(6), the compiler converts the int 6 into an Integer 6, thus it would match the sifter(Object) method.

提交回复
热议问题