Friday, May 27, 2005

Applications

"Real Programmers don't write application programs, they program right down on the bare metal. Application programming is for feebs who can't do system programming."
(Vleck 1982).

Introduction

Here you will learn what an application is, and about the different parts of an application. Later you will learn how to write and save an application using a text editor, how to compile an application and how to run an application.

Program

Any Java application must follow this structure:

// declare package after this line

// import classes or packages after this line

class JavaApplication {
    public static void main(String[] args) {
        // enter code after this line
    }
}

An application that follows this structure is the HelloWorld application, a typical "first program" printed in most text books:

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

Output

This program is supposed to print "Hello World" to the screen, but does nothing much yet, because it has to be compiled and executed first.

Discussion

Now, you will get a quick tour of the code.

Also remember that Java is case-sensitive!

Types of Applications

Such Java applications are running in a terminal window in text mode. It is possible to extend this application to build much more sophisticated applications, such as GUI applications and applets, by importing parts of the Java library. Read about Swing for GUI applications (reference needed).

class

class HelloWorld {
}

This part of the code is called a class declaration, and contains the parts of the class called the class members which is actually the rest of the program code. Be aware that a program can have many classes.

Now, consider the HelloWorld application. Java programs seems impossible to apprehend at first sight. You should always do the simplest thing that works, right? Would it not be much simpler just to write:

println("Hello World!");

Why do we have to write all that other stuff? The main reason for the extra code in Java, is that Java forces us to use object-oriented programming (OOP). This program isn't much OOP-ish, but even so OOP dictates that all code should go inside classes.

main

The method main is where the Java interpreter will start the program execution. The Java Virtual Machine assumes that the class contains a method called main, where execution will begin, and that will take the program parameters called args.

public static void main(String[] args) {
}

It is a convention to write method main as public static void main(String[] args) {, but you could also write:

  public static void main(String args[]) {
  public static void main(String[] arguments) {
  static public void main(String[] args) {

Note: Many classes in a Java application may contain a main method. This means that a Java application may have more than one possible starting point.

public

Consider the code:
public class JavaApplication { // unusual
    public static void main(String[] args) {
        // enter code after this line
    }
}

You don't normally need to use access modifiers like public for the class containing the main method. However, Netbeans IDE 5.0 declare its Main classes as public. It is not necessary to declare the the class public. The Java environment executes the main method anyway.

static

static is used so that main can be called without first making an instance of the class. That is, main is a class method.

void

The keyword void simply means that the method main does not return anything.

String[] args

Consider the code:
    public static void main(String args[]) { // not so good

This code works, but you should write String[] args and not String args[], because it is easier to see that args is a String array.

println

System.out.println("Hello World!");

This is the statement that prints "Hello World!".

References

No comments: