Lesson 1

INTRODUCTION TO JAVA PROGRAMING



What is Java?

Java is an object-oriented programming language developed by Sun Microsystems. It is modeled after C++ but designed to be a small, simple, and portable across platforms and operating systems. It is platform independent, which means it can be run on any computer system, provided the system has a Java virtual machine installed (you will learn about this later). Java can be used to write stand alone applications just as can any other computer language. What has made Java so popular is its ability to create Applets which can be used on Web pages.

An Applet is a mini-program that can be downloaded from the World Wide Web by a Web browser and run inside an HTML page. An Java-enabled browser must be used. Netscape 2 or higher and Navigator 3 or higher are Java-enabled browsers. Such browsers can down load and run Java applets on the user's system. Applets appear on Web pages in a way similar to images. However, Applets are not static like images, they are dynamic and interactive. They can be used to create animations, forms that respond to the user's input, figures, games and other interactive applications.

You create an applet by writing it in the Java language, compile it using a Java compiler and then refer to it in your HTML Web page. This is then uploaded to your Web site like any other HTML and image files. When someone using a Java-enabled browser looks at your Web page containing the embedded applet, the browser will download the applet to his system and execute it. This allows your viewer to see and interact with your applet. If someone is using a browser that is not Java-enabled, they will see text, a static picture, or nothing. You should be aware that Netscape nor Explorer will run Java applets on Windows 3.x.

Why Learn Java?

The most compelling reason you have for learning Java at this moment is to avoid getting a failing grade in this course. The other reason is probably that applets for Web pages are written in Java. However, Java also has significant advantages over other languages. It is platform independent. This means that a program you write in Java can be run on any computer system. This is true at both the source and binary level.

At the source level, Java's data types maintain consistent sizes across all development platforms. Its class libraries make it easy to write code that can be moved from platform to platform without the need to rewrite or revise it. Code written in Java does not rely on features of a particular operating system to accomplish basic tasks. This allows you to transport your Java source code from system to system, compile it and have it run perfectly on any system.

Normally when you compile a program compiled in Pascal, C or some other language, the compiler translates your source code into machine code specific to the processor your computer is using. For example, if you compile your code on an Intel-based system, the resulting program will only run on other Intel-based systems. If you wanted to run your program on another system (i.e., Apple), you have to get a compiler for that system and recompile your original source code for that system.

When you write code in Java, things are different. The Java development kit consists of two parts: A Java compiler and a Java interpreter (the Java Virtual Machine). Your Java source code is converted into what is called bytecode by the compiler. Bytecode is machine language that is not specific for any processor. To run a Java program, you run a program called a bytecode interpreter. This program (Java Virtual Machine) reads the bytecodes and executes your program. A bytecode interpreter comes with the Java development kit. If you are running applets, the bytecode interpreter is built into your browser (Netscape 2 or Explorer 3) which you need to run applets.

Getting the Java Development Kit

Although Netscape and Explorer allow you to run Java applets, they do not let you write them. For that you will need the Java Development Kit (JDK) available from Sun Microsystems at http://www.javasoft.com/. The complete kit comes in a single self-extracting file. The kit contains over 100 files organized in several subdirectories. For Windows 95 you should down load the file JDK116-Win32.exe. Once you have down loaded the file, run it to create the complete Java Developer's Kit. Sun JDK is somewhat primitive. Its tools are executed from the DOS prompt. The Java compiler is a program called javac.exe. It is found in a subdirectory called BIN. Unless you add its location to the path statement in your autoexec.bat file, you will have to type the path to execute it.

To make things easy for yourself, you should tell your computer where to find the Java files. To do this you edit your AUTOEXEC.BAT file. Assuming you have install the JDK on the C: drive in a subdirectory called JAVA, add \JAVA\BIN to the path. Also you need to set an environment variable named CLASSPATH. If you are using Windows 95 do the following:



1. Use Notepad to open C:\AUTOEXEC.BAT

2. Locate the line that reads SET PATH=

3. Add to this line: C:\JAVA\BIN;

4. If the line SET CLASSPATH= exists, add this to it:

C:\JAVA\LIB\CLASES\CLASES..ZIP

If the line does not exist add:

SET CLASSPATH=C:\JAVA\LIB\CLASES\CLASES.ZIP

5. Save the revised file, AUTOEXEC.BAT

6. So the revised AUTOEXEC.BAT file takes affect, reboot your computer.



Check it out

Look at the contents of the JAVA/DEMO subdirectory. Under JAVA/DEMO, you will find several subdirectories (i.e., Animator, Barchart, Blink, TicTacToe, etc.). You can use your Web browser to view some of these example files. Start your browser, click on FILES, OPEN FILE and type in the path: C:\Java\demo\TicTacToe\example1.html. Now click OPEN. You should see a Java version of Tic Tac Toe. You can play a game if you wish.

There are other development kits available that have graphical user interfaces with integrated editors and debuggers. If you can afford one you will find it much easer to write, compile and debug Java code.


Creating a Java Application

The time has come to actually write some Java code. We will start where everyone else starts -- the classic program Hello World. What we are going to create is a Java application, not a Java applet. Our program will not require a browser to execute it. However, a single Java program could be an applet or an application, or both, depending on how we write the program and the capabilities the program uses. You probably want to learn to write applets, but be patient. Everything you learn writing simple Java applications will apply to creating applets. Applets are a little more difficult to write than applications and we want to do the easy stuff first.


Creating the Source Code

The Java source code, as with all languages, is created using a plain text editor. We will use Windows NotePad. Open NotePad and type the following:

class HelloWorld
{
 public static void main (String args[])
	{
	 System.out.println("Hello World!");
	}
}

After you have finished typing your program, save it with the name HelloWorld.java. It is important to note: Java source files MUST have the same name as the class they define. Also, you must use the same upper and lowercase letters; Java is case sensitive. If you name your file helloworld.java, you will not be able to compile it. Java source code files must have the extension .java.

Compiling the Source File

We will use the Java compiler that comes with the JDK. To run the compiler, you will have to start the DOS shell (MS-DOS Prompt). From inside DOS, go to the directory where you saved your HelloWorld.java file. Once you are in the correct directory, type the following:

javac HelloWorld.java

Now press the <ENTER> key and if your did not make any mistakes, you will be rewarded with a file called HelloWorld.class. This is your Java bytecode file. To run it you need the Java bytecode interpreter. Its name is JAVA. To run your program, type after the DOS prompt:

java HelloWorld

Now press the <ENTER> key, and you will see Hello World! on the screen. Note that we did not type the extension .class after HelloWorld. If you do, your program will not run.

Be sure you understand that the Java compiler and the Java interpreter are to different programs. You use javac (the Java compiler) to transform your Java source code into .class files. You use java (the interpreter) to execute your class files.

Creating a Java Applet

Java applets are created to be run and displayed inside a Web page. Because of this they must follow special rules which requires more complex source code. For example, An applet that displays Hello world on a Web page has to do more than just print a message. It must also reserve space for the message on the Web page and use special font and graphics operations to paint the message to the screen.

Writing the Source File


//Hello World Applet
import java.awt.Graphics;
public class HelloWorldApplet extends java.applet.Applet
{
 public void paint(Graphics g)
	{
	 g.drawString ("Hello World!", 5, 25);
	}
}


Create the code above using a plain text editor (i.e., NotePad). Save the file the same way as you did the Hello World application. Remember, the file name must be exactly the same as the name of the class. In this case the class name is HelloWorldApplet, therefore, the file name should be HelloWorldApplet.java.

Compiling the Source Code

Although this is an applet, you compile it exactly the same way you saved the Java application. From in side the DOS shell, go to the directory containing your applet source code. Type the javac command followed by a space and HelloWorldApplet.java. If you did everything correctly, you will end up with a file called HelloWorldApplet.class in the same directory. This is your Java applet file.

Inserting the Applet in a Web Page

To run the applet inside a Web page you must refer to that class file inside the HTML code for that page using the <APPLET> tag. Type the following HTML code:

<HTML>

<HEAD>

<TITLE>Test Hello</TITLE>

</HEAD>

<BODY

<P>Look at my Java applet:

<APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25>

</APPLET>

</BODY>

</HTML>


Save the HTML code as tstapplt.html.

We will learn more about <APPLET> latter, but for now remember the following:

  • The CODE attribute is used to name the class that contains your applet, in this case HelloWorldApplet.class.


  • The HEIGHT and WIDTH attributes indicate the size of the of the applet on your page. These dimensions are used by the browser to set aside space for the applet. In this case a box 25 pixels by 150 pixels is created.


Now comes the moment of truth. We will run and view our applet. To view the applet you need a browser that supports Java applets such as Netscape 2.0 or Explorer 3.0 or later versions.

If you are using Netscape or Explorer, click on FILE and then on OPEN FILE. Type the address and file name of your HTML file: tstapplt.html and click on open. If all went well, you should see something like the illustration below:

Summary

This lesson has given you a basic introduction to the Java language, its objectives and features. It is a programming language similar to C++ which can be used to create a large variety of programs. The most popular use of Java at this time is the creation of applets for the World Wide Web. Applets are mini-Java programs that are downloaded and run as part of a Web page. Applets can be used to create animation, games, interactive programs, and other visual effect on Web pages.

One of Java's strong points is its portability at both the source and binary level. It is also an object-oriented language. It can be used to write general purpose applications as well applets.

We have created an application using the java language (a stand alone program) and an applet. You have learned to use the Java compiler (javac) and the Java interpreter (java).

Copyright (C) 1998 by J. Auciello