Ok, so I was doing an exercises today that asked me to create an application that would display every date in a given year. I solved the exercise however when I looked at the text book answer I found that they had acheived it using a different (and more elegant) solution. The code snipped below shows the start of their solution. The rest is done with a switch.
... public static void main(String[] arguments) { int year = 2011; if (arguments.length > 0) year = Integer.parseInt(arguments[0]); for (int month = 1; month < 13; month++) for (int day = 1; day <= countDays(month, year); day++) System.out.println(month + "/" + day + "/" + year); } ...
I completely understand everything except...
if (arguments.length > 0) year = Integer.parseInt(arguments[0]);
My confusion here is 2 fold! Firstly this is the first time I have seen any reference to "arguments.length" (or args.length). What "argument" is this refering to? Where is the length being determined from? and why is it in an if statment to determine whether the value is greater than 0? The fact that it has square brakets after it ([]) in the second line suggests that its refering to an array, but none is declared.
Secondly... according to the API "Integer.parseInt(String)" parses the string argument as a signed decimal integer. But year is already an int variable anyway.