This tutorial will get you started in the Java programming language. It is intended for individuals with prior programming experience who need an introduction to the Java programming environment.
Java is a programming language and a platform. Unlike languages like C or Pascal, which can be compiled for multiple operating systems and machines, Java programs only run on the Java platform. It just so happens that the Java platform has been implemented for a variety of systems which is how Java claims to be "write once - run anywhere".
The Java platform consists of a set of libraries and a Java Virtual Machine (JVM). Java code is compiled into the assembly language for the JVM. This assembly language is referred to as bytecode. Technically, it is not really assembly language -- it still requires all sorts of run-time linking by the JVM -- but the concept of machine formatted instructions is very similar.
The Java platform is distributed as the "Java Runtime Environment" (JRE) and the "Java Development Kit" (JDK). The JRE has all the libraries and executables needed to run the JVM. The JDK contains every thing the JRE has, plus the programs needed to compile, debug, and document a Java program. There are seperate JDK and JRE distributions for different platforms.
The JRE and JDK are available from Sun Microsystems. At the time this was written, the quickest way to the distributions follow:
You will see several versions of Java are available. Following is a description of the versions available at the time of writing. I am assuming that you will be programing in Java so I will only be discussing the JDK from here on out. If you just want to learn Java, I recommend getting the latest final release (currently 1.3).
Download one of the releases. Note that there is a source release available for some of the platforms. If you download this, you will exclude yourself from working on any open-source Java implementations. I wouldn't do it unless you have a pressing need to change the source or fix a bug. The remainder of this tutorial will assume that you are working with JDK1.3. Follow the instructions on Sun's site to install the JDK. If you are running on Windows, I recommend installing to "C:\jdk1.3". The rest of the instructions will make this assumption. If you are running on Unix, I will assume that you are smart enough to know where you want to install it and how to change the instructions I provide. You should also download the documentation. Unzip it into C:\jdk1.3.
The installation location contains the file src.jar. This contains most of the source code for the platform's libraries. The source can be very useful when you are trying to find out what a class is doing and how it interacts with your code (often much more useful than the documentation). Unjar the file with the following command:
Take a look in the installation location. The following sub-folders are important right now:
At this point, I feel like it would be natural to explain how the tools in the bin directory work. I am going to skip ahead to, what I see, as the most important concept to understand when you are moving to the Java platform. I have seen development houses miss this for years. Understanding this will pave the way for an extremely easy transition into Java.
In Java, everything that you write will be in a class. In general, there is only one class per file and the class name must be the same as the filename. A class with the name "Foo" must be in a file named "Foo.java". This convention is enforced! If you have a class in a file with a name different than the class, the compiler will complain.
Every class is also defined in something called a package. Packages are a technique to give you some control at organizing your class files. Namespaces are unique between packages, so it also lets you name your classes with less fear that you will conflict with someone else. A class "Foo" in package "mypackage.stuff" would be referenced as "mypackage.stuff.Foo".
Packages are defined hierarchically. The swing libraries are in the package javax.swing and the sound libraries are in javax.sound. Don't let this heirarchy fool you though. Each package is distinct in of itself. The heirachy is only in the mind of the programmer. Java recognizes no special relationships between children or siblings of packages. It is best to think of them as namespaces that organize your code in a logical way. There is also a root package. A class "Foo" in in the root package would be referenced as just "Foo".
Just as classes must be defined in files with like names, packages are defined by directories with like names. The class "mypackage.stuff.Foo" will be in "somedir/mypackage/stuff/Foo.java" where "somedir" is a path representing the location of the root package. When Java is looking for the class "mypackage.stuff.Foo", it first looks for a directory called "mypackage" in "somedir", then "stuff" in "mypackage", then "Foo.class" in "stuff". Java knows to look in "somedir" by using something called a "classpath".
The classpath tells Java where to look for root packages. The classpath can have multiple elements and the elements are searched in reverse order. If the classpath is defined as "C:\mypackage\;C:\otherpackage\" and Java is looking for the class mypackage.stuff.Foo, it will first look for Arcs.class in C:\otherpackage\mypackage\stuff\Foo.class and then C:\mypackage\mypackage\stuff\Foo.class. Note that on Unix systems, each element of the classpath is separated by a colon (':') instead of a semi-colon (';'). The separator for a particular system is actually defined in Java as "File.pathSeparator".
The elements of a classpath can consist of directory locations and jar files. JAR is an abbreviation for Java Archiver (which is a slight change from the Unix tool tar - tape archiver). A jar file contains a list of class files in a directory structure. It is very similar to a zip file and can even be compressed. The root contents of a jar file are considered to be a package root. Jar files are a convenient way to distribute applications with large numbers of classes and packages. They can even be compressed to minimize the time spent downloading them if containing an applet (compression does increase the load time however). The "jar" program in the jdk bin directory can be used to create and decompress jar files.