3. Using Arguments and String Arrays
Let's go a little step further and use the start-up arguments assigned in the main method to learn how you can use arrays, check the array length, and loop through them.
We need to create another new Java file and call it
args variable of the main method is a String array and we will use it with the following code:
Now we can start the application and provide it any number of extra arguments.
From the array, we can get the number of items (=
args.length) and use a for-loop for cycling through all the arguments.
$ java MainArguments.java Number of arguments: 0 $ java MainArguments.java "Hello World" "Bye" Number of arguments: 2 First argument: Hello World Argument 1: Hello World Argument 2: Bye