CIS 3020 Part 9
Jump to navigation
Jump to search
Command Line Arguments
- When you execute a Java program, you can provide values directly to the program
- These values are called "command line arguments" since they are in the command to execute your program:
java Print2CLArgs 123 hello
class Print2CLArgs { public static void main (String[] arg) { ... System.out.println(arg[0]); System.out.println(arg[1]); ... } }
- Remember: the command line arguments are strings
- Suppose we execute:
java PrintSum 3 5
- And PrintSum contains:
System.out.println(arg[0] + arg[1]);
- What will be output?
35
- What you need to write is:
int i1 = Integer.parseInt(arg[0]); int i2 = Integer.parseInt(arg[1]); System.out.println(i1 + i2);