Passing Parameters to a Java Program – Building Blocks – 1Z0-829 Study Guide

Passing Parameters to a Java Program

Let’s see how to send data to our program’s main() method. First, we modify the Zoo program to print out the first two arguments passed in:

public class Zoo {

public static void main(String[] args) {

System.out.println(args[0]);

System.out.println(args[1]);

}

}

The code args[0] accesses the first element of the array. That’s right: array indexes begin with 0 in Java. To run it, type this:

javac Zoo.java

java Zoo Bronx Zoo

The output is what you might expect:

Bronx

Zoo

The program correctly identifies the first two “words” as the arguments. Spaces are used to separate the arguments. If you want spaces inside an argument, you need to use quotes as in this example:

javac Zoo.java

java Zoo “San Diego” Zoo

Now we have a space in the output:

San Diego

Zoo

Finally, what happens if you don’t pass in enough arguments?

javac Zoo.java

java Zoo Zoo

Reading args[0] goes fine, and Zoo is printed out. Then Java panics. There’s no second argument! What to do? Java prints out an exception telling you it has no idea what to do with this argument at position 1. (You learn about exceptions in Chapter 11, “Exceptions and Localization.”)

Zoo

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:

Index 1 out of bounds for length 1

at Zoo.main(Zoo.java:4)

To review, the JDK contains a compiler. Java class files run on the JVM and therefore run on any machine with Java rather than just the machine or operating system they happened to have been compiled on.

Single-File Source-Code

If you get tired of typing both javac and java every time you want to try a code example, there’s a shortcut. You can instead run

java Zoo.java Bronx Zoo

There is a key difference here. When compiling first, you omitted the .java extension when running java. When skipping the explicit compilation step, you include this extension. This feature is called launching single-file source-code programs and is useful for testing or for small programs. The name cleverly tells you that it is designed for when your program is one file.

Understanding Package Declarations and Imports

Java comes with thousands of built-­in classes, and there are countless more from developers like you. With all those classes, Java needs a way to organize them. It handles this in a way similar to a file cabinet. You put all your pieces of paper in folders. Java puts classes in packages. These are logical groupings for classes.

We wouldn’t put you in front of a file cabinet and tell you to find a specific paper. Instead, we’d tell you which folder to look in. Java works the same way. It needs you to tell it which packages to look in to find code.

Suppose you try to compile this code:

public class NumberPicker {

public static void main(String[] args) {

Random r = new Random(); // DOES NOT COMPILE

System.out.println(r.nextInt(10));

}

}

The Java compiler helpfully gives you an error that looks like this:

error: cannot find symbol

This error could mean you made a typo in the name of the class. You double-­check and discover that you didn’t. The other cause of this error is omitting a needed import statement. A statement is an instruction, and import statements tell Java which packages to look in for classes. Since you didn’t tell Java where to look for Random, it has no clue.

Trying this again with the import allows the code to compile.

import java.util.Random; // import tells us where to find Random public class NumberPicker {

public static void main(String[] args) {

Random r = new Random();

System.out.println(r.nextInt(10)); // a number 0-­9

}

}

Now the code runs; it prints out a random number between 0 and 9. Just like arrays, Java likes to begin counting with 0.

In Chapter 5, we cover another type of import referred to as a static import. It allows you to make static members of a class known, often so you can use variables and method names without having to keep spec-ifying the class name.