2

The Java main method must be passed an array of Strings (traditionally named args). That is, the signature of main must be

 public static main(String[] args)

or

 public static main(String... args)

That is the constraint on the code that an application programmer must write. But is the runtime environment, which "calls" your main method allowed (according to the language or JVM specification) to provide null references for args. That is, is it valid for the runtime environment to start your program in manners equivalent to the following:

 MyClass.main(null);

 MyClass.main(new String[]{null})
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • 4
    No. You would not get `null`. You would get an empty `String[]`. There is no way to pass a Java `null` at the command line. – Elliott Frisch Jan 30 '18 at 22:25
  • 1
    @ElliottFrisch No way using Linux or Windows executing Java in the normal manner, using the common `java` program. But that is not the only permitted manner of starting a Java program allowed. This question has the [tag:language-lawyer] tag for a reason. – Raedwald Jan 30 '18 at 22:29
  • And how would the null then be passed to the entry point? The signature is what it is because it maps to the traditional C entry point. Although, language-lawyer, it must return `void` in Java (and it's bad form to omit the return type in C too - where it wouldn't take a `String`). – Elliott Frisch Jan 30 '18 at 22:31
  • I think there is a similar question: https://stackoverflow.com/questions/12358603/java-args-array-in-main-method-confusion-null-check – Bizon4ik Jan 30 '18 at 22:33
  • @ElliottFrisch The runtime environment can construct the initail call to the `main` function in many possible ways, It might be s special driver class in Java, which would have a call like the examples I give. It might actually be C++ code that stuffs C `structs` that onto the stack. – Raedwald Jan 30 '18 at 22:35
  • 1
    @EugenePodoliako Good catch. I'd failed to find that question. – Raedwald Jan 30 '18 at 22:37

0 Answers0