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 hereStudent: 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, what width to use for it. So I could say
MakeFlag.java
$ java MakeFlag.java 400and it would make a flag of width 400, but if I wrote
$ java MakeFlag.java 200the 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?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.int width = Integer.parseInt(args[0]);int width = args.length;args.lengthis how many arguments there are, not what the first one says.
How many elements does
args have for this command?$ java MakeFlag.java 300 blueFor that same command, what is
args[1]?"blue""300"That isargs[0]; indexes start at0."MakeFlag.java"javauses the file name to find the program; only what comes after it goes inargs.- Nothing:
MakeFlagstops with an errorExtra arguments are allowed.MakeFlagnever looks atargs[1], so it makes a flag 300 wide.
What happens here?
$ java MakeFlag.java- It stops with an
ArrayIndexOutOfBoundsException:argshas length0, so there is noargs[0]. - It makes a flag of some default widthNothing sets a default;
widthcomes only fromargs[0]. - It does not compileIt compiles fine. Java can’t know how many arguments a run will get until the run starts.