Your first Java program
Finally, heres the first complete program. It starts by printing a string, and then the date, using the Date class from the Java standard library.
// HelloDate.java
import java.util.*;
public class HelloDate {
public static void main(String[] args) {
System.out.println("Hello, it's: ");
System.out.println(new Date());
}
}
At the beginning of each program file, you must place the import statement to bring in any extra classes youll need for the code in that file. Note that I say extra. Thats because theres a certain library of classes that are automatically brought into every Java file: java.lang. Start up your Web browser and look at the documentation from Sun. (If you havent downloaded the JDK documentation from java.sun.com, do so now[13]). If you look at the list of the packages, youll see all the different class libraries that come with Java. Select java.lang. This will bring up a list of all the classes that are part of that library. Since java.lang is implicitly included in every Java code file, these classes are automatically available. Theres no Date class listed in java.lang, which means you must import another library to use that. If you dont know the library where a particular class is, or if you want to see all of the classes, you can select Tree in the Java documentation. Now you can find every single class that comes with Java. Then you can use the browsers find function to find Date. When you do youll see it listed as java.util.Date, which lets you know that its in the util library and that you must import java.util.* in order to use Date.
If you go back to the beginning, select java.lang and then System, youll see that the System class has several fields, and if you select out, youll discover that its a static PrintStream object. Since its static, you dont need to create anything. The out object is always there, and you can just use it. What you can do with this out object is determined by the type it is: a PrintStream. Conveniently, PrintStream is shown in the description as a hyperlink, so if you click on that, youll see a list of all the methods you can call for PrintStream. There are quite a few, and these will be covered later in this book. For now all were interested in is println( ), which in effect means print what Im giving you out to the console and end with a newline. Thus, in any Java program you write you can say System.out.println("things"); whenever you want to print something to the console.
The name of the class is the same as the name of the file. When youre creating a standalone program such as this one, one of the classes in the file must have the same name as the file. (The compiler complains if you dont do this.) That class must contain a method called main( ) with this signature:
public static void main(String[] args) {
The public keyword means that the method is available to the outside world (described in detail in Chapter 5). The argument to main( ) is an array of String objects. The args wont be used in this program, but the Java compiler insists that they be there because they hold the arguments from the command line.
The line that prints the date is quite interesting:
System.out.println(new Date());
The argument is a Date object that is being created just to send its value (which is automatically converted to a String) to println( ). As soon as this statement is finished, that Date is unnecessary, and the garbage collector can come along and get it anytime. We dont need to worry about cleaning it up.