Appendix B: Getting Started with Java

If you've never programmed before, welcome to the world of Java programming. Java is a programming language. What that means is that you can write programs with this language that can be used to control a computer. Java is a high level programming language. A high level programming language is different than a low level programming language in that the higher level a programming language, the further removed it is from the actual machine language commands that drives the computer's actions.

Programming is all about using computers to solve real world problems. Along the way, people found that it is also useful to make games and other things that accompany people's use of computers. By learning programming, you are also learning how to describe algorithmically how systems work and interrelate. These logical analysis skills will be sharpened by learning programming, and your programming will get better through learning.

Assuming that you have a computer, become familiar with it. The computer is your friend. The computer can only do what it is told to do, so the more eloquent you can make your communication with it, the better your relation will be. Some parts of your computer may seem very arcane, and the computer may seem a "black box" which is impossible to scry. This is not true. The computer is deterministic.

Find out how to work with files on your computer. Figure out how the directories or otherwise how files and information is stored and located. Get used to creating, moving, copying, and deleting files. You should be comfortable with your computer.

The most basic tool in your programming arsenal is a text editor. This is the program that makes it possible for you to convert text (which humans can read) to files (which computers can read). The text editor you use is important. It is up to you to make the decision which text editor to use. There are many text editors. Some text editors check your spelling automatically and can whistle. That is not what you want. You want a text editor that makes text files and has some features that work really well, and has small enough of a footprint to allow you to use without taking many of your system resources. Choose your text editor wisely, but don't worry, you can always choose a different text editor later.

Another thing to be aware of are environment settings for your computer. These are normally run from the the Control Panel on a Graphical User Interface (GUI) heavy platform like Mac or NT. Or more text based systems these are normally run from your startup shell or autoexec. For our purposes, environment settings are just references to directories.

The first step in learning to use Java is to found out more about it. The best way I have found to accomplish this is to go right to the source, and see what those associated with Java think of it. There are a variety of ways to get information about Java and programming in general. Perhaps the most powerful way is to use the Internet to find information.

At this point in time, you should have some kind of access to the Internet. While the Internet can not at this time replace a well stocked library, it is the only way to get access to the newest information that is available. Generally, printed matter is six months to a year behind the state of the art of Internet information on Java. As a beginner, you don't have to jump to the leading edge immediately, but you will find the access provided through the Internet most useful.

Is your computer big enough to run Java? There is a simple metric to apply to determine this. If your web browser supports Java, most likely you can develop Java on the same computer. If you can not find a web browser that supports Java for your computer, you might be out of luck and be unable to run the Java compiler programs on your computer. However, you can still use your computer to create Java programs and learn and research more about programming, and later apply your skills on a bigger computer.

Find a web browser that supports Java. There are a variety of these on the available. Get used to your browser, and start to use the web if you do not already. Go to your favorite search engine and look up Java. Browse around to help you get a feel for what Java means in relation to other programming things on the Internet. One good place to go is the Yahoo! directory, www.yahoo.com. They have kindly organized a large variety of programming related web sites. By browsing their topic hierarchy you can see those places where Java information is located, and find out more from there.

Now, go to this site, www.sunsite.edu/javafaq. This site has daily information updates about a lot of different Java news. It might mean little or much in context, but will help give you an idea of what people are working on.

Point your browser to Javasoft's web site, www.javasoft.com. This is "the" Java home page. The most definitive information regarding Java is available from this site. You will notice that Java(tm) is a copyright of Sun Microsystems, Inc., the parent company of Javasoft. What does that mean to you, the programmer? Not much really. You don't have to pay Javasoft to create, use, or deploy Java programs. If you build Java technology like a web browser in another language to support Java, you can license it from Java or create your own version from the specifications, called a "clean room" implementation. Don't worry about this.

You will see a variety of things. Read around. Now that you are looking at this web site, congratulate yourself and welcome yourself to being a Java developer. The next step is to get the tools that will let you build Java applications. There are many places to get tools to develop Java applications, Javasoft is not the only one. We like to use Javasoft products if only because they are the ones least likely to be displaced by Java. For more programs you can use to make Java, go to an Internet search engine and look up "Java Integrated Development Environment".

The next step is to look at some Java applets, to see what Java can do. There are a variety of sample applets on Javasoft's web page that give you an idea of some of the things that Java can do. A web search for "Java applets" will also turn up a bunch more. After you have seen some applets, then you can rest assured your web browser is functioning and go to the next step.

The next step is to get the development tools for your computer. There are two critical applications you need to create and use Java applets and applications. The first is Java compiler program. A compiler takes a file written in the Java language and creates a Java byte code class file. The next application is a runtime application. The runtime application executes a Java class file to run a Java application. Most development kits have a variety of other development tools as part of their kits, such as appletviewers to view applets without a full scale web browser, and other tools to support some of the other features of the language. What the development kit really boils down to is the compiler. This is the program that will make your Java class files from the Java language files.

Choosing the right development tools is a question of your computer type and your personal preferences. The recommended way to go is to use Javasoft's Java Development Kit, the JDK.

After you get a development kit, the next step is to install the kit on your computer. Make sure your computer has enough storage space for the development kit files. Follow the kit's installation instruction. Essentially, this is a matter of executing the kit's installation executable and choosing where on your file system to put it.

Once the executable has finished installing, you are pretty much ready to go. Look through the directories that have been created. There will be a variety of things. One of the nicest things with the JDK is a bunch of example applets with source code. Use the appletviewer to try the demonstrations. Look at the source code files with your text editor and comapre them to what the applets do.

Now that your JDK is installed, go back to the web. At Javasoft's site there are the JDK documentation and an online tutorial. If you have enough space on your computer, download the documentation so you can browse it offline. Open your web browser and start reading the documentation. There is a lot of it, but it is generally well presented. Don't worry about trying to comprehend it all at once, the newest stuff represents the leading edge of programming and will take a while to understand.

One thing to take special note of in the documentation is the Javadoc documentation. Javadoc is a program that takes commented source code and creates browsable HTML pages with inormation about the Java class and it's relationship to other Java classes.

You can now see that Java classes are often grouped in sets called packages. A package is a set of Java classes that are related in functionality. Not only are packages useful ways to keep large numbers of classes organized, they also serve a technical purpose. Some classes and their methods can only be accessed by method calls fom classes in the same package.

Use the tutorial. It is mostly straightforward, and offers a quick introduction to most of the features of the language. In fact, it is recommended that you visit the tutorial often until you feel have grasp of the language and how it works.

By this time, you might feel more confortable buying or borrowing some more books about Java programming. There are quite a few Java books, thanks for buying or borrowing this one.

Now it is time to compile a Java program. Open your text editor. Create a new file if it is not already created. Type the following into your text editor:
public class FirstApplication{	public static void main(String[] args){		System.out.println("FirstApplication has been executed.");	}}
The spacing doesn't matter. Use the spacing to make it easier for you to follow. If you are using an IDE it might space your source code automatically. That is normally good too. Please note that most everything is case-sensitive.

Save your file with this name: FirstApplication.java. Case is important, that is it can not be saved as FiRsTAppliCation.java.

Open a shell prompt and go to the directory where you saved your file. Using your java compiler, compile your application. Normally it is like this:
javac FirstApplication.java
The compiler will print some messages . If it can not compile, it will tell you so and why. If that happens probably you mistyped something. When the compiler completes, the class file FirstApplication.class will be created in the same directory. Execute it now by typing:
java FirstApplication
If you get the error message "Can not find..." most likely the directory is not in your machine's classpath. Classpath is an environment variable. What that is is a value that programs running on your computer can access, like the location to look for Java class files.

The method to set an environment variable depends on your computer. On UNIX machines it is normally set in the shell login with setenv. On Windows machines it is normally set in autoexec.bat or the control panel for NT. The classpath environment variable is similar to the path variable in that the compiler and interpreter will only execute code that is within a directory listed in the classpath.

At this point, you have compiled and run your first Java program. Congratulations!
JFC Manual Table of Contents
This document may be viewed and printed for personal or educational use. It may not otherwise be redistributed, transmitted, or stored in electronic, paper, or other form without the written consent of the author. The diagrams contained herein may be redistributed, transmitted, or stored as long as they are not modified. For print and redistribution requirements contact Ross A. Finlayson, raf@tiki-lounge.com. Java (tm) is a copyright of Sun Microsystems Inc. © 1998 Finlayson Consulting All rights reserved.