| Author |
casting error (K&B)
|
Rajesh Chandra
Ranch Hand
Joined: Jun 13, 2005
Posts: 55
|
|
Hi Pals, I found this question at a mock test from (K&B)K&B Mock Q5- 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)); } } What is the result? A). 1 1 B). 2 1 C). 3 1 D). 4 1 E). 1 3 F). 2 3 G). 3 3 H). 4 3 The answer given is H.Isnt it wrong for how primitives can be implicitly casted to Wrappers? it should have been compile time error. regs Rajesh [ October 29, 2005: Message edited by: Rajesh Chandra ]
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hey Rajesh, Can you give a little more detail about what you think is wrong? Thanks, Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Rajesh Chandra
Ranch Hand
Joined: Jun 13, 2005
Posts: 55
|
|
Hi Bert, Well the calling method doX(s,s) and doX(7,7) has no counterpart methods with required arguments to call.How can doX(s,s) call int doX(Number n, Number m) and doX(7,7) call doX(Integer x, Integer y) ?? Anyway I get this compile time error C:\j2sdk1.4.1\bin>javac Eggs.java Eggs.java:12: cannot resolve symbol symbol : method doX (short,short) location: class Eggs System.out.print(doX(s,s) + " "); ^ Eggs.java:13: cannot resolve symbol symbol : method doX (int,int) location: class Eggs System.out.println(doX(7,7)); ^ 2 errors Im at loss whats happening here. Thanks Rajesh
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hey Rajesh, It seems that you're using a Java 1.4 compiler, not a Tiger compiler. The mock questions that we posted are meant to be testing your knowledge of the Tiger compiler, not the 1.4 compiler. hth, Bert
|
 |
Rajesh Chandra
Ranch Hand
Joined: Jun 13, 2005
Posts: 55
|
|
thanks ! indeed Im using java1.4.So theres huge turnaround in java1.5?? Never expected this.Anyway thanks again regs Rajesh
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: casting error (K&B)
|
|
|