Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting error (K&B)

 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rajesh,

Can you give a little more detail about what you think is wrong?

Thanks,

Bert
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ! indeed Im using java1.4.So theres huge turnaround in java1.5??
Never expected this.Anyway thanks again
regs
Rajesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic