These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

JAVA01/General Structure

Classnotes | JAVA01 | RecentChanges | Preferences

The general structure for any computer program is to have some main function which is ran by default. This main function (or segment of code) is the basic one which, in turn, runs everything else. In many programming languages, this main function is, in fact, called main, and this is no different in Java.

Let's look at a rudimentary Java source:

 class Hello {
      public static void main(String[] arguments) {
          // My first Java program goes here
      }
 }

Each line of this source code is called a statement. The class statement is the way you give your computer program a name. It's also used to determin other things about a program, but we will cover that later. In this program, the name "Hello" should match the name of the file "Hello.java". As a general rule, a Java program should have a name that matches the name of its file.

The main statement tells the computer that the main function starts here. There are many extra qualifiers here, but for now we will merely say that they are required and reserve a proper definition for later.

The brackets signify that what is contained in them should be taken as a single block of statements. So here, the contents of the brackets are the block of statements associated with the main function. You can embed blocks of statements inside of other blocks of statements by using additional brackets. For example,

 public static void main(String[] argv) {
      ....
      if( v == 0 ) {
          ....
      }
 }

One of the most common errors you will have will be unmatched brackets. Be sure that every bracket you write as a mate somewhere else (do not leave any open or dangling brackets!)

The '//' line is a comment. When the computer comes to a commented line, it will ignore everything after the '//'. This is a place where you can enter comments for yourself (or other people) to see. Comments are very important, and well-commented code cab be much easier to read and debug.

In order to call a function, in Java you must refer to it using it's full class line. For example, to print something to the screen, the statement to use would be:

 System.out.println("I am text to the user!");

Here, we are calling the "println" function, which is part of the "out" subclass of the "System" superclass. Note that we end the statement with a semi-colon ';'. This is a required method for ending a statement, and virtually every statement in your program (excluding comments, which are simply ignored) must end in a semi-colon.



Classnotes | JAVA01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited May 27, 2003 8:37 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.