Command-Line Arguments
- A Java application can be passed String arguments when it is executed.
- These are called
commandline arguments
and allow your programs to receive simple input from whomever is executing
your program.
- When you execute your application, you can pass in values by adding
them to the end of the java command:
java MyFamily Pippi
- Note: Figure out how to do this in your IDE.
- Each separate string is an argument, so if you want to pass in a phrase,
(i.e. "Pippi the dog") you will need to quote it.
- In your program, the array named args has the passed-in arguments.
- (Remember
static void main(String args[])
?)
- To access these values in your program, you just access the array.
- The first argument is in args[0].
- Unlike Linux Shell, C, and C++, the first element in the array does not contain the name of the program.
- Here's a code snippet showing the basic idea: