next up previous contents index
Next: Classes Up: Java Previous: Java   Contents   Index

A simple program

We'll jump right into a simple program:
//file Hello.java
public class Hello {
   public static void main(String[] args) {
       String name = args[0];
       int age = Integer.parseInt(args[1]);
       if(args.length != 2) {
           System.out.println("Usage: Hello name age");
           System.exit(1);
       }
       System.out.println("Hello " + name + ". You are "
                           + age + " years old.");
   }
}
Compiling this program involves
   javac Hello.java
which creates a class file, and then
  java Hello Harry 44
runs the executable. There are a few things worth noting about this example: Note also the use of args.length to check for the length of the args array. This reminds us of an object.method syntax call to invoke a method, and indeed that is the case here - as one gets deeper into Java, one finds that many fundamental entities one works with have an underlying class structure.
next up previous contents index
Next: Classes Up: Java Previous: Java   Contents   Index