Re: Use of Integer.parseInt
Quote:
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.
The String array "arguments" is in the first line of the code. It holds any command line arguments in a String array.
Code :
public static void main(String[] arguments) {
Quote:
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.
The variable "year" may be an int, but the call to the parseInt method converts element 0 of the arguments array to a int.
Re: Use of Integer.parseInt
Quote:
Originally Posted by
tarkal
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.
What is the parameter to the main method? It is a String array called arguments. All arrays have a public instance variable called length that holds value equivalent to how many elements the array can hold.
Code java:
int[] arr = new int[10];
System.out.println(arr.length);
That will display 10.
So where does the String array parameter to the main method come from? The command line.
Code java:
class CommandLineArguments {
public static void main(String[] args) {
for(int index = 0; index < args.length; index++) {
System.out.println(args[index]);
}
}
}
Run the above program from the command line with the follow command: java CommandLineArguments one two three four
The output will be:
one
two
three
four
Quote:
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.
Yes year is an int and as explained above the elements stored in the arguments array are Strings. Therefore you need to parse the String into an int before you can store it in an int variable.
Re: Use of Integer.parseInt
Thanks guys. Interestingly not one of my books actually explains the main method argument (at least not yet). They all just say to "accept it as this is how main methods are constructed".
Thanks for the little program Junky, it was really really helpfull. I had no idea that the string array parameter could be passed to the main method from the command line. It now all makes perfect sense, including why Interger.parseInt is present, given that the value being returned from the command line is being stored as a String.