☰ Contents Collecting Values

Arrays and main

Student: Wait a minute. I just realized I’ve seen String[] before.
Prof: Oh really? Where?

Student: Well, um, my friend told me that the way you do main in this book is all wrong. What she learned looked more like

public static void main(String[] args) {
  ...
}
Prof: Oh, interesting. How long has your friend been a Java programmer?
Student: Oh she’s um, actually, my friend is uh… well, AI wrote that when I asked it to do the homework. I want to know what it does.
Prof: Gothca. Well, your “friend” isn’t totally off base, she’s just a few versions behind. That’s how you had to write Java until a few years ago; the short void main() is new. And this is actually a great time to talk about main and String[] args so I’m glad you brought it up.
Student: What about public static?
Prof: Not quite the right time. Remember, learning is—
Student: Yeah yeah a series of successively smaller lies and omissions. OK fine tell me about String[] in main.
Prof: OK so you know how we type things like java MyProgram.java at the terminal?
Student: Right, like in the homeworks.

Prof: That’s right. So we can put more stuff after the name of the java file. For example, we can write

$ java MyProgram.java more stuff here
Student: OK, why would you do that?

Prof: A lot of programs use it to pass in extra options or configuration to a program that gets started from the terminal. They are called command line arguments. I have an example in mind – imagine we want to write a program that runs from the terminal and creates a flag image, let’s re-use Bangladesh because the image is simple to make. However, we want to say, when we run java
MakeFlag.java
, what width to use for it. So I could say

$ java MakeFlag.java 400

and it would make a flag of width 400, but if I wrote

$ java MakeFlag.java 200

the width would be 200.

void main(String[] args) {
  int width = Integer.parseInt(args[0]);
  int height = width * 3 / 5;
  Pict sun = new Circle(width / 5, "red");
  Pict cloth = new Rect(width, height, "green");
  Pict flag = new Overlay(sun, cloth);
  System.out.println("Bangladesh, " + width + " by " + height);
  ImageIO_.show(flag);
}
$ java MakeFlag.java 400
Bangladesh, 400 by 240
$ java MakeFlag.java 200
Bangladesh, 200 by 120
$ 
Student: Oh that’s super interesting. Cool. What’s Integer.parseInt?
Prof: We need to use that to turn a String into an int. If we left it out, we’d get an error from Java that width is a String, not a number, when we try to use * or / with it.
Student: OK, so it’s another built-in method?
Prof: Exactly, and we use it a lot when we want to use data that comes in from outside our program. The word parse usually means to turn a String into another kind of structured data. Another thing to notice – the arguments are everything after the filename, the "200" is at index 0. The other stuff on the command line, like java and the filename, are not included in args.

Practice

args is a String[]. Which line gets the first command line argument as an int?
  1. int width = args[0];
    This does not compile: incompatible types: String cannot be converted to int. "400" is text, even when it looks like a number.
  2. int width = Integer.parseInt(args[0]);
  3. int width = args.length;
    args.length is how many arguments there are, not what the first one says.
How many elements does args have for this command?
$ java MakeFlag.java 300 blue

For that same command, what is args[1]?
  1. "blue"
  2. "300"
    That is args[0]; indexes start at 0.
  3. "MakeFlag.java"
    java uses the file name to find the program; only what comes after it goes in args.
  4. Nothing: MakeFlag stops with an error
    Extra arguments are allowed. MakeFlag never looks at args[1], so it makes a flag 300 wide.
What happens here?
$ java MakeFlag.java
  1. It stops with an ArrayIndexOutOfBoundsException: args has length 0, so there is no args[0].
  2. It makes a flag of some default width
    Nothing sets a default; width comes only from args[0].
  3. It does not compile
    It compiles fine. Java can’t know how many arguments a run will get until the run starts.