Home
<<Table of Contents
Variables in Java>>

Java / pg. 1
INTRODUCTION TO JAVA

Welcome to the web's top resource on Java programming. In this attractive, hard-cover edition you will find all you need to learn Java. So, get cracking, and good luck!

  1. Getting Started
  2. Compiling and Embedding Your Applet
  3. Debugging "Hello World"
  4. Understanding the "Hello World" Applet
  5. Understanding the HTML

Top

Getting Started
Java is a platform-independant programming language developed by Sun Microsystems. It was designed to create programs that can be run by any computer. Java is compiled into a code that the client's computer compiles into the language native to the computer at run-time. Although it is not the ideal language for most applications, platform-independence makes it perfect for the Internet. Special Java programs called applets can be embedded into HTML and run by Java-Enabled browsers.
Although Java is capable of creating programs that can be run outside of a browser, this web site only discusses applets. If your goal is to learn how to create applications with Java that run without a browser, this is a good place to start, but keep in mind that programming applications in Java is very different from programming applets. Java is actually very similar to C++. Java uses the same syntax and structure as C++, but is has different commands and lacks some of C++'s more advanced features such as pointers. The traditional way to begin learning a programming language is the "Hello World" application, and that is how we will start.

//The Hello World Applet!!!
//These are comments.
/*Comments are ignored by the compiler.
The two methods of inserting comments are displayed here.*/

import java.applet.Applet; //These are compiler directives
import java.awt.Graphics;

public class HelloWorld extends Applet { //The program begins here
public void paint (Graphics g) {
g.drawString ("Hello World", 5, 15);
}
} //This is the end of the program

Top

Compiling and Embedding Your Applets
This program will display the words "Hello World" in the browser window, but first, it must be compiled. You can use the free compiler provided by Sun, or you can use one of the more expensive compilers. I am a tight-wad, and I use a free compiler. The more expensive compilers give you the same results and take the same input, but they are more user friendly. If you do opt for Sun's compiler, you can download it from their site, "
HTTP://Java.Sun.Com." It is called the Java Development Kit (JDK), and this version (1.0) is not the latest, but you must register with them to receive the latest version. Also, most browsers do not support the features offered in the latest version of the JDK. After you download the JDK, I strongly recommend that you download the API Documentation. You will probably be using it very often during all of you programming endeavors. Java.Sun.Com also contains the latest java info.
Now that you have a compiler, follow the directions of the compiler to compile your program. I will not explain compiling in this site because it varies on different computers and compilers. If you manage to compile without any errors, you will get a file named HelloWorld.class. If you have problems compiling, remember that the file with the source code must be named "HelloWorld.java" which is the name of the class. If it still won't compile, there is no need to worry. There are a lot of things that can go wrong, and it is not essential to do this project right now. If you do get a HelloWorld.class file, you must embed it into an HTML file before you can run it. Here is the necessary code.

<HTML>
<Head><Title>The Hello World Applet</Title></Head>
<Body BGColor=#FFFFFF>
<applet code=HelloWorld.class width=70 height=20>Your Browser does not support Java</applet>
</Body>
</HTML>

Now, just put "HelloWorld.class" and your new HTML file (the name does not matter) in the same directory, and open the HTML file in the browser of your choice. If all goes well, you will be treated to a lovely gray box with the words "Hello World" printed in it. If your browser displays the words, "Your browser does not support Java," that means that your browser does not support Java (Surprise, Surprise). If you were not so lucky, the applet is below.

Your Browser does not support Java

Top

Debugging "Hello World"

Isn't that just amazing! OK, it isn't THAT amazing, but it is your first Java applet. If the words "Hello World" are not you idea of an amazing program, fear not. This will get more exciting, but first, we must make sure that everybody has compiled their first Java program. If your program refused to compile, do not worry. Java is very strict. First, make sure that all code is exactly the same. Java is case-sensitive. "Graphics" is not the same as "graphics." Also, make sure you included all semicolons (;) and brackets ({}). As for the HTML, browsers are very flexible and are not case-sensitive. Just make sure that you do not have any silly mistakes (Everybody makes silly mistakes). Also, make sure that the file with the source code is named "HelloWorld.Java," and the .class file is in the same directory as the HTML. Sometimes, hitting the refresh button on browser will not update an applet. Whenever you recompile an applet, exit the browser and reload the page. If none of these tips help, check the documentation for you compiler.

Top

Understanding the "Hello World" Applet
Now that everybody has compiled their first Java applet (Congratulations!), we must analyze it. The problem with Java is that no other programming language has a "Hello World Program" that is nearly as complicated. Maybe we should learn basic instead. Here is the Hello World Program in Basic

10 Print "Hello World"
20 End

No? OK, we can learn Java. The first lines in the "Hello World" applet are comments. Comments are ignored by the compiler, they have no effect on the compiled program. As a matter of fact, if you remove all the of the comments from the source code and compile it, it should give you a file that is totally identical to the first one. There are two ways to make comments. The first is the most common. You just precede the comment with two forward slashes (//). Here is an example.

//This is a comment. I hope you enjoy it.

You can also have a comment after a command.

g.drawString ("Hello World", 5, 15); //This is also a comment.

The second method for creating a comment, is the multi-line comment. You begin the comment with a forward slash and an asterisk (/*), and end it with an asterisk and a forward slash (*/). Here is another example.

/*AMAZING, this is also a comment. Tell the neighbors!
INCREDIBLE, this comment has several lines, but it does not have to. It can also be one line.*/

Anything inside the /* and */ is ignored by the compiler. Comments are very useful so you can keep track of your program when it gets larger (Try "Hello Universe!") or when you plan to give it to somebody else. Comments are also very useful for people who design pages that explain Java.
The next line (import java.applet.Applet) is only slightly more complicated. It is a compiler directive. Compiler directives are always the first lines of a Java program (except comments which you may put anywhere). They are just like C++'s compiler directives. For those of you who do not know C++, compiler directives tell the compiler what to do. By itself the compiler can not do anything. The directives tell it to load commands that can be used for the program.
The next line (public class...) is also very simple. This tells the compiler that the class is beginning. Now everybody, take out last night's homework assignments (groan). Seriously, Java's classes are very similar to C++'s. For those of you who do not know C++, classes are explained in another section. They are not very important now. In Java, each file has one class. In this class, the word "public" tells the compiler that other classes may access it freely which is irrelevent in this case. The "extends Applet" part tells the compiler that this class is an applet. To make a class an applet, you just add "extends Applet" to the class section, but remember, you must have the "import java.applet.Applet;" compiler directive. This helps you understand what directives are a little more. If you do not add any compiler directives, the program is not capable of very much. Finally, the open bracket ({) signifies the beginning of the class.
The next line also very simple. It is the beginning of a sub-program (sub) or Method (The official Java lingo), or whatever the heck you want to call it. It is the paint sub. Subs (or method or whatever-the-heck they are called) are miniature little programs that can be called from other sub's (or whatever) and classes. The first word "public" tells that the compiler that any other sub or class is allowed to call it. The word "void" tells the compiler that this does not return a value. Subs that return values are discussed in another chapter. Paint, of course, is the name of the sub, and the parentheses ((Graphics g)), contain parameters that must be passed on to the Sub when it is called. In this case, a graphics context must be given to the sub paint whenever it is called. Finally, the { is the beginning of the sub. The sub paint is special because whenever the applet must be refreshed (because it is moved or another object goes over it) this event is called and a graphics context is given to it. By changing this context, you can draw on the applet by doing this. Java is a strange language because you can only draw on the screen when this sub is called (or when the update sub is called, but that is not important now). This is very frustrating at first, but it starts to make sense when you get better at Java.
The next line (g.drawString...) is the line that does the dirty work. It actually calls a sub (method). This sub is located in the class Graphics which must be included in the compiler directives. Actually, "g" is a Graphics class which is passed to the paint sub by the applet. "drawString" is a sub inside of the class Graphics. The dot operator (a period) tells the compiler to look for the sub inside of the class "g" which is a Graphics class. The parentheses contain the parameters that must be passed to the drawText sub. The first parameter is the string to print, and the second two parameters are the coordinates of the baseline of the text. Finally, the semicolon (;) tells the compiler that the line is ended. These must be included at the end of every command and compiler directive, or else the compiler gets confused. When I began programming in Java, I forgot almost every semicolon. Actually, I still forget semicolons.
Finally, we have the last two lines. They are both close brackets (}). The first is for the end of the paint sub, and the second is for the end of the class. Brackets specify blocks of code. The open bracket, shows the beginning of a block of code (such as a class or sub), and the close bracket specifies the end.
There is one more thing worth mentioning. The tabs are not required by the compiler. Most programmers put them their to make the program easier to read. Actually, the compiler ignores almost all white space (returns, tabs and spaces).

Top



Remember: Always place something inside the applet tags to provide support for people without Java. You may include pictures and tags.

Understanding the HTML
Well, you now understand most of the Java code, but we are still not finished (groan). Stop you moaning, remember, I had to write this thing. You try to write a document that explains a programming language and not be a little boring. It is important that you know how to embed a Java file into an HTML document. Most of the code in the applet tag is self-explanatory. The code, width, and height tags are obvious. One strange thing about the applet tag is the </Applet> tag. It seems pointless to have two applet tags, but there is a good reason. You may notice that the words "You browser does not support Java" are inside the two tags. Browsers that support Java, ignore any text inside of these tags, but older browsers do not know any better. They ignore the applet tags, which baffle them, and display the text. This allows you to provide support for older browsers. If you have an older browser, you have seen this firsthand where the "Hello World Applet" is supposed to be. If that is the case, you missed out on an incredible gray box with the words "Hello World" printed in it. Another reason for the two tags are parameters. Applets can accept parameters, and they are put inside of the two applet tags.


Home
<<Table of Contents
Variables in Java>>

Congratulations, you are now a Java programmer, but don't get too cocky. There is more to come (groan). So get some rest, grab a snack and report back here for the rest of your training. If all of this bewilders you, do not worry. Java is simple, but it is big. You were presented with a lot things that were not explained. They will be explained later, and all of this will become clear. I hope that you find you newfound Java skills to be useful.