Java Reference
In-Depth Information
Listing 1-3: Reading arguments
package com.apress.java7forabsolutebeginners;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, " + args[0] + "!");
}
}
■
Note
Computers start counting at 0 rather than 1. Consequently, the first member of an array can be found at
0. Typing
args[1]
here generates an out-of-bounds exception, by which Java means that it expects to find two
strings, but you provided only one. You'll quickly get used to computers starting their counting at 0.
System.out.println
accepts a single
String
object as its argument. In this case, we've got three
String
objects, but the plus signs concatenate them together to create a single string, satisfying the
requirement (for just one string) of the
println
method. The plus sign is Java's string concatenation
operator (in addition to being a plus sign when used for mathematical operations). We cover operators
in Chapter 4, “Operators.”
To provide a value for the argument in Eclipse, follow these steps:
1.
From the Run menu, choose Run Configurations. The Run Configurations
window appears. Figure 1.6 shows the Run Configurations window.
Figure 1-6. The Run Configurations window.
2.
In the Arguments tab, type your name.
3.
Click the Run button.