Monday 18 March 2013

SCJP - 28 -Given Example


What is the result?
A. 1 1
B. 2 1
C. 3 1
D. 4 1
E. 2 3
F. 3 3
G. 4 3

Answer:

G is correct. Two rules apply to the first invocation of doX(). You can’t widen and then box
in one step, and var-args are always chosen last. Therefore you can’t widen shorts to either
ints or longs, and then box them to Integers or Longs. But you can box shorts to Shorts and

then widen them to Numbers, and this takes priority over using a var-args method. The
second invocation uses a simple box from int to Integer.

A, B, C, D, E, and F are incorrect based on the above. (Objective 3.1)
==
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(long... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
} }


No comments:

Post a Comment