What will be the result of the following code execution?

class Alien {
  String invade(short ships) { return "a few"; }
  String invade(short... ships) { return "many"; }
}
public class Defender {
  public static void main(String [] args) {
    System.out.println(new Alien().invade(7));
  }
}
Explanation
Constant 7 has an int type by default. When the function is called an implicit narrowing conversion int → short is not performed, so the compilation error will occur.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback