What is the output of the below program?
class MyClass {
public static void main(String h...) {
System.out.println("Hello world");
}
}
Before you scroll down, think about it. The main method does not use the usual String[] args. It uses String h.... Does the JVM accept it?
The correct output is: Hello world
The program compiles and runs. It prints Hello world.
Why this works
The JVM looks for a method named main with these four properties:
publicstaticvoidreturn type- A single parameter of type
String[]
At the bytecode level, String... and String[] are the same thing. Varargs is a compile-time feature. The compiler converts String h... into String[] h before producing the class file.
So when the JVM inspects the class, it sees:
public static void main(String[] h)
That matches the required signature. The JVM accepts it. The program runs.
Why the answer is not compile error
Many developers assume the JVM only accepts String[] args exactly. It does not. It accepts any parameter whose type resolves to String[]. Varargs is one such form.
Why the answer is not runtime error
Nothing in this program fails at runtime. The method is valid. The print statement is valid. The output is just the string.
Should you write main(String h...) in real code?
No. Use main(String[] args). The varargs form works, but it offers no advantage and confuses readers. Every Java developer expects the array form. The parameter name can be anything you want (args, h, argv), but the type should be String[].
Common variations interviewers ask next
| Variation | Result | Why |
|---|---|---|
public static void main(String[] args) |
Compiles, runs | Standard signature |
public static void main(String h...) |
Compiles, runs | Varargs compiles to String[], JVM accepts it |
public static void main(String args) |
Compile error or missing main | Parameter is String, not String[] |
public void main(String[] args) |
Compiles, but JVM says "main method not found" | Method is not static |
public static int main(String[] args) |
Compile error or missing main | Return type is int, not void |
static void main(String[] args) |
Compiles, but JVM says "main method not found" | Method is not public |
The rule to remember
The JVM is strict about four things in the main method: public, static, void, and String[]. Everything else is flexible. The parameter name does not matter. The varargs form is accepted because it compiles to String[].
If you remember one thing, remember this: varargs is just an array in disguise. The JVM never sees the three dots. It sees String[]. That is why this program works.
Where to go next
You now know why main(String h...) compiles and runs, and why you should still write main(String[] args) in real code.
If you want to go deeper, try changing the return type to int and run it. Watch the JVM print Main method not found in class MyClass. Then change it back to void and make it non-static. Watch the same error appear. Each change teaches you one of the four rules the JVM enforces.
No comments:
Post a Comment