Written by David Reilly
Revised May 12, 1999
Previous | Next
Editor's Note : When I first wrote this tutorial series, in late 1996, Java was a relatively new language. It had a lot of potential, but a relatively small following by today's standard. In rewriting this tutorial series, I was amazed at how things had changed. Java is now recognized as an excellent object orientated language, is used widely in commercial development and educational institutions. Still, many things have remained the same. Its speed, for example, is still a problem for commercial software applications - though this too is changing, and perhaps when I look back in two more years time, such problems will be rectified.
-- David Reilly, May 1999
Java quickly became a hot buzzword of the computing industry. People wanted to know Java - it was said to be great for creating dynamic interactive content for webpages. Yet the true power of Java lies not in applets, but in its many other uses. Java is used for developing standalone applications, and for server-side programming. The face of Java has changed, but the core language remains the same. In this tutorial series, I'll teach you the basics of Java programming. You'll still need a good book as a companion to this tutorial series, but for those who are dabbling in Java, this should be enough to get your feet wet.
Before I begin to cover the basics of Java programming, I should point out that Java is an object-orientated language, and may not be suitable for first time programmers. Learning a new language takes some time, but learning your first object-orientated language can be exceedingly difficult. Nonetheless, if you've done some C programming before, the shift into Java shouldn't be unreachable, providing you obtain a good reference book. There are also those that believe programmers should start with an object-orientated language first, and many universities have adopted this practice. Still, you've been fairly warned :)
This tutorial will presume that you have some basic programming knowledge, particularly in C, as it will not be covering such principles as sequence, selection and repetition. If you are unsure on ' for ' loops, or complex ' if ' statements, I'd suggest coming back here at a later point.
Java software comes in several flavors - the most common being the stand-alone application, and the applet. Web developers may have come across the term applet before, and perhaps even used one. An applet is an piece of software code that runs under the control of a web browser, as distinct from the application which requires an interpreter.
Applets are commonly used to enhance the interactivity of a web page, and deliver client-side content. Applets run in their own frame, and can display graphics, accept input from GUI components, and even open network connections. Due the potential security risks associated with running applets from external and potentially malicious sources, most web browsers limit file access, and impose additional restrictions on applets (such as only being able to connect to the hostname from which the applet was downloaded).
Fortunately, stand-alone applications have no such restrictions, and a full range of functionality is provided for in the way of pre-written Java classes. Stand-alone applications can run as a console application (writing text to the screen or terminal window), or they can have a graphical user-interface, by opening a new window or dialog box. You've used applications before, such as word processors, text editors, and games. The Java language is capable of all this things.
Since stand-alone applications offer more freedom to the programmer, and applets running under a browser often demonstrate a certain degree of instability depending on the platform under which it is run, this tutorial series will concentrate primarily upon the stand-alone application.
The first thing required for writing stand-alone Java applications is a java compiler/interpreter. While there are commercial offerings available, such as Visual J++ and Borland JBuilder, a freely available SDK is available from Sun, the original creators of the Java language. It contains a compiler, interpreter, debugger, and more. I highly recommend using the latest version of Sun's Java Development Kit (JDK). You should download the JDK from http://java.sun.com.
After downloading and installing the Sun Java SDK for your required platform (Windows 95, NT, or Solaris), its time to write, compile, and run your first Java application - the obligatory "Hello World".
class myfirstjavaprog
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
Listing 1.0 - myfirstjavaprog.java
The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial.
To compile your first java application, enter the following (assuming that the java directory is in your path) :
javac myfirstjavaprog.java
Javac is a compiler included with Sun's JDK. It translates the source file into Java byte-codes. While Java is an interpreted language, it is reduced into byte-codes which are interpreted by a Java virtual machine (in much the same way assembly language / machine code for older computing systems can be executed by emulator software). The compiler stores these byte-codes in a ' .class ' file. To execute the application, the Java interpreter will run this ' .class ' file.
java myfirstjavaprog
If everything goes according to plan, the message "Hello World!", followed by a newline should appear on your terminal/screen. You've just compiled and executed your first application.
For those new to object-orientated programming, the concept of a class will be new to you. We defined a new class, called myfirstjavaprog. Simplistically, a class is the definition for a segment of code that can contain both data (called attributes) and functions (called methods).
When the interpreter executes a class, it looks for a particular method by the name of main, which will sound familiar to C programmers. The main method is passed as a parameter an array of strings (similar to the argv[] of C), and is declared as a static method (more on this in a later tutorial).
To output text from the program, we execute the ' println ' method of System.out, which is Java's output stream. Unix users will appreciate the theory behind such a stream, as it is actually standard output. For those who are instead used to the Wintel platform, it will write the string passed to it to the user's screen.
That wraps it up for this first part of the introduction to Java tutorial series. In the next tutorial, we'll cover some more object-orientated principles, and extend your knowledge of the Java language and syntax.
Previous | Next