aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting???? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Casting????" Watch "Casting????" New topic
Author

Casting????

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
In k&b book,




What is the result?
A. many
B. a few
C. Compilation fails.
D. The output is not predictable.
E. An exception is thrown at runtime.

Answer is C,
And explanation was given as: C is correct, compilation fails. The var-args declaration is fine, but invade takes a short,
so the argument 7 needs to be cast to a short. With the cast, the answer is B, 'a few'.


But following line compiles fine
short s = 7;

My question is why cast is necessary while passing int as parameter in invade(7) method, I thought that 7 is implicitly converted to short??


Sage of The Monstrous Toad of Mount Myoboku
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
while passing arguments casting isnt done....we have to provide them ourselves
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
see this
http://www.javaranch.com/journal/200508/Journal200508.jsp#a2
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Thanks Ankur, I got it
 
I agree. Here's the link: jrebel
 
subject: Casting????
 
Similar Threads
SCJP 5.0 K&B Book,Chapter 3 Qes no3 page no 267
scjp5.0 question
small doubt in declaration
small doubt in declaration
Assignment question