Help on Java for CSCI Students


Last revised 16 January 2007, 11:50 am

The Java programming language was developed by Sun Microsystems. For downloads and comprehensive documentation on Java, check out the Sun Microsystems Java website at http://java.sun.com. You should especially get acquainted with their Java Tutorial.

As you learn about the various features of the Java language, you should test your understanding by writing lots of small test programs. You can write Java programs (including standalone "applications", "applets", "servlets", or whatever) using a wide variety of tools, ranging from primitive text editors like Microsoft Windows Notepad to elaborate Integrated Development Environments (IDEs) such as the NetBeans IDE.

The Sun SDK itself provides low-end development tools suitable for use in a command-line environment. For completeness, and because they can occasionally be quite useful, these tools are described briefly in the following.

Once you have written or downloaded a Java source file (which incidentally must have a .java suffix), you will need to compile the source to produce the bytecode (.class) file which can be run by the Java interpreter (aka the Java Virtual Machine, or JVM). While the .java source files are really just textfiles, the .class files are binary files which cannot be printed or displayed on the screen.

For command-line users, Sun provides a free standard edition of the Java Software Development Kit (SDK) which contains both a compiler and interpreter. Incidentally, you should note that the SDK is still known by some oldtimers as the Java Development Kit (JDK). The SDK and the NetBeans IDE have both been installed under Linux on the PCs in the Computer Science Department Laboratory in Robinson 004. Of course you can download the SDK and NetBeans yourself if you prefer to work on your own PC.

If the SDK is installed on your system of choice, you can always compile a Java source file named, say, Hello.java, by entering the console command

> javac Hello.java

If the compiler detects no errors in your source code, it will produce a .class file with a matching name, Hello.class. Of course, if it does detect errors, you will have to try to understand what its error messages are complaining about. It can, and usually does, take several rounds of editing and compiling before you can advance to the next stage...

What happens next depends on whether your source code represents a standalone Java application, or an applet, which is run by a Web browser (more precisely, by a Java interpreter contained within the browser). All the simple console demos are applications, or apps for short, so it makes sense to consider this case first.

To invoke the Java interpreter and make it load and run the .class file for an application, enter the java command followed by the filename, without the .class suffix:

> java Hello

If the application expects command-line arguments, they follow the class name as shown in the example below:

> java Factorial 4

Finally, as you learn more about Java and slowly become acquainted with its extensive class libraries, you should start making regular use of Sun's on-line Java 2 SE API Documentation.