main and its argumentseasy0-2 years

A method starts with `if (args == null) { ... }` to guard against no arguments being passed, and a reviewer flags it as dead code that will never run. Are they right, and what actually determines what ends up in `args`?

The reviewer is right — args is empty when nothing is passed, never null. java Greet with no arguments gives a zero-length array, not a null reference, so the check will never trigger. Two other things worth knowing about what's actually in args: the program's own name is not included — unlike C, where argv[0] is the program itself, in Java args[0] is the first argument — and the shell, not Java, decides how the text after the class name gets split into separate strings. java Greet "two words" arrives as one argument because the shell removed the quotes and passed a single string before Java ever saw anything; Java has no idea quotes were involved.

The lesson behind it →