0

如果我错了,请纠正我。Boxing+Varargs 是否优于 Boxing+Widening?

我在一个网站上发现它是另一种方式。

4

3 回答 3

2

What method is called when several could qualify is defined in the JLS #15.2.2:

The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.

The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.

So in summary: widening > boxing&unboxing > varargs

于 2012-09-20T10:24:23.233 回答
0

Boxing+Widening 优于 Boxing+Varargs。一个简单的测试将证实这一点。

public static void main(String[] args) {
    int i = 2;
    doX(2);
}

static void doX(Object i){
    System.out.println("object...");
}
static void doX(Integer... i){
    System.out.println("int...");
}

印刷:

object...

编辑:对不起,我的错。我已经更正了代码。没有注意到我输入了“对象...”

于 2012-09-20T10:16:36.617 回答
0

Boxing + Widening优于Boxing + Varargs. 我更改了@John 的示例以显示这一点:

public static void main(String[] args) {
    int i = 2;
    doX(i);
}

static void doX(Object i) {
    System.out.println("Object");
}

static void doX(Integer... i) {
    System.out.println("Integer...");
}

印刷

Object
于 2012-09-20T10:22:11.543 回答