QuestionQ232

Working with Methods and Encapsulation

Given:

Question Image

What is the result?

  • A int main 1
  • B Object main 1
  • C String main 1
  • D Compilation fails
  • E An exception is thrown at runtime
Explanation

The Java launcher looks specifically for a method with the exact signature public static void main(String[] args) to use as an application's entry point; overloaded main methods declared with an int[] or an Object[] parameter are valid, distinct overloads that compile without error, but they are never chosen by the JVM as the entry point regardless of how the program is invoked. Running java MainTest 1 2 3 therefore invokes only the String[] overload, and since the command-line arguments are passed as strings, args[0] is the string "1", so the program prints "String main 1".

Community Discussion

No comments yet. Be the first to start the discussion!