These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

JAVA01/Command Line Arguments

Classnotes | JAVA01 | RecentChanges | Preferences

In the original example, you'll recall we had a 'String' declaration next to the 'main' function. This was no ordinary string, it was declared as an array (or an array of strings). (We will examine arrays in more detail later on)

 class Hello {
      public static void main(String[] arguments) {
          // My first Java program goes here
      }
 }

As we shall see later, this is the way to pass arguments to a function. Since this is our main function, the arguments that are passed to the function come from the command line.

For example, if we ran our program thusly:

 C:\> java Hello foo bar -k

then our String would have the values:

  • "Hello" for the first value
  • "foo" for the second value
  • "bar" for the third value
  • "-k" for the fourth value

The way to reference these strings is to use the square brackets "[ ]" with a number inside of them. So, for the above example, arguments would be set to these:

 arguments[0] = "Hello"
 arguments[1] = "foo"
 arguments[2] = "bar"
 arguments[3] = "-k"


Classnotes | JAVA01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited May 27, 2003 9:27 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.