File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes small doubt in declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "small doubt in declaration" Watch "small doubt in declaration" New topic

small doubt in declaration

shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
I know this question is very simple but i have a basic doubt.In the below code invade(7) what is the variable 7 by default declared a int..

class Alien {
String invade(short ships) { return "a few"; }
String invade(short... ships) { return "many"; }
class Defender {
public static void main(String [] args) {
System.out.println(new Alien().invade(7));
} }
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 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'.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Yes, whole number literals are ints, provided they are in the range of an int.
Ramamoorthy Periasamy
Ranch Hand

Joined: Feb 06, 2006
Posts: 30
Thats what you need to understand the difference between method invocation conversion and assignment conversion.

Assignment conversion includes the narrowing primitive conversions.
example: short s = 10; // no error
Method invocation conversion doesn't include the narrowing primitive conversions
example: method(short s) {} method(10); // compile time error
I agree. Here's the link:
subject: small doubt in declaration
It's not a secret anymore!