Welcome to The Basics

Let start by looking at some Java code. The following code is for a stand-alone application. Stand-alone applications are more like traditional C programs. They have a main(), and they can be run without Netscape.

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

Let's analyze this code.
First, we declare a class called HelloWorld. Java code must be saved in a file that has the same name as the main class, followed by .java. If there is more than one class in the file, the file must be named after the class that will be run first. For instance, HelloWorld.java would the be filename for this file.

NOTE: The class name and the filename are case sensitive.

Next we declare the function main() just as in C. public means that it can be called by anyone, and static means that there can only be one of them. Notice main()'s arguement list. The array of Strings called args takes the place of argv and argc from C and C++. Also, in Java the args is required in stand-alone applications, unlike the optional argv and argc in C amd C++.

The only thing the main() function does is print "Hello World". System.out.println is the Java syntax for printing text.

Also worthy of note is that Java does not require a return value when the return type is void. The main() function does not even have a return statement.

Again, this code was for a stand-alone application. If we were to write it as an applet, the syntax would be a little different.

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

The first lines of the program are import statements. When you write an applet, you can tell the compiler which packages you'll be using so that you don't have to write the full name for every class. For example, the applet class is java.applet.Applet. However, since we imported java.applet.*, we can simply refer to it as Applet. Similarly, the graphics class is java.awt.Graphics. Since we imported java.awt.*, we can just say Graphics.

Next, we define our class, same as before. "extends Applet" means that HelloWorldApplet is a child of Applet, so it will inherit from Applet.

Now, since we're not creating a stand-alone application, we don't need to declare a main() function. Instead, applets have a set of functions which define how they work. init(), run(), start(), and paint() are some of them. So, since our applet just draws a couple of words, we only need to define the part that does the drawing, or painting, paint().
paint() is public, but it is not static. Each object of this type needs it's own copy of paint(). And, the return type is void.

The Graphics variable g represents the graphics layout for the entire applet. It is created in the background for us and passed to paint() when it is called b ythe browser. g.drawString says to draw the string at the given position in the Graphics layout g, which is the one for our applet.

Now that we have an applet, we have to include it in a page. Here's how:

<APPLET CODE="HelloWorldApplet" WIDTH=150 HEIGHT=80> Here you can put text to be displayed if the browser doesn't understand java. </APPLET>

Here you can put text to be displayed if the browser doesn't understand java.

Now that we've seen some Java code, we're going to talk a bit about Java syntax and semantics.

First of all, Java is object-oriented. Everything you write in Java will likewise have to be object-oriented.
What is an object? An object is an instance of a class.
What is a class?
A class is the basic building block of Java. Every program you write will consist of one or more classes. A class is a group of data and the functions (called methods) that work on that data. We talk more about this later.

To declare a class, you use the following syntax:

public class ClassName [extends SomeClass] [implements SomeInterface] { }

The public keyword says that this class can be used by anyone. Only in rare cases will you ever want to declare a class anything other than public.
ClassName, SomeClass, and SomeInterface are names of calsses. ClassName is the name of the class we're defining. SomeClass is the name of the class we are inheriting from, if any. SomeInterface is the name of the class we are implementing, if any. We'll talk about inheritance and interfaces a little later.

Everything between the curley braces will act like a self-contained little program. Unless declared as public, nothing in the class will be visible to any other class. This feature is called Data Hiding. It enforces a level of abstraction, and it prevents accidental modification of data.

Let's look now at SimpleApplet.

import java.applet.*; public class SimpleApplet extends Applet { private String line_to_print = "Hello World"; public void start() { showStatus(line_to_print); } }

This simple applet (hence the name) just displays "Hello World" in the status bar at the bottom of the browser.

You can see that we declare a class SimpleApplet. It is public, as usual, and it inherits from the class Applet, which is really java.applet.Applet. (See Above.) All applets must have Applet somewhere in their family trees.

Inside of SimpleApplet we declare a variable, line_to_print. line_to_print is of type String. Strings are a basic variable type in Java. They are not, however, primitve. All of the primitive types begin with lowercase letters (int, float, char,...). Everything else is actaully a class. So, String is a class that acts just like a data type.
Anyway, line_to_print is of type String. We write the variable declaration just like in C++, except that we can declare it public, private, or a handful of other things we'll talk about later. Declaring it public gives everyone access. Declaring it private gives no one outside of the class access. line_to_print is declared private, so only methods inside of SimpleApplet can access it.

Also inside of SimpleApplet we declare a method, start(). We declare it as public with a return type of void. By declaring it public, it can be accessed from outside of this class.
Declaring a method in Java is almost the same as declaring a function in C++. The only difference is that it can be declared as public, private, etc. just like when declaring a variable.

Why did we call our method start()? Because start() is one of the basic methods all applets have, and it gets called at the start.
When an applet is loaded onto a page, the browser calls a series of methods in the applet to make it run. These methods have to be public so that the browser can see them.
First the browser calls init() to do any initializtions. Next it calls start(). After start() it calls paint() to let the applet display whatever it's going to display. After that, the applet is on it's own.
A few other methods get called when specific things happen. When the user leaves the page, stop() is called. If the applet gets damaged (covered up, etc.) and needs to repaint itself, repaint() is called. And there are event handlers which will react to keyboard input, mouse clicks, etc..
We'll see more of how all of these are used later.

O.K. By now you should be overloaded with new information if you've never coded in the OOP before. If you're an old hand at OO C++, then all of this should look pretty familiar.
If you need to, go back and read this section again. When you're ready to go on, the next lesson is Using Classes. We'll talk more about classes, particularly the Applet class.

Next Lesson: Using Classes

Return to the Main Menu.