Small Java Programs

This chapter discusses naming and coding conventions as well as reserved words in Java. When you go through this chapter, you'll get some hands-on experience with writing in Java.

4. Between the Braces


ExcitingGame     OK: mixed upper and lower case is allowed
Lady Luck     BAD: no spaces allowed inside an identifier
x32     OK: identifiers must start with an alphabetical character, but the rest can be digits.
lastChance     OK: class names usually start with a capital, but identifiers don't have to.
x/y     BAD: the slash is not allowed inside an identifier
1stPrize     BAD: starts with a digit

Between the Braces

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

The small Java programs in this chapter all look like this:

public class ClassName

Everything that a program does is described between the first brace and the final brace of a class. To start with, we will have only one class per source code file, but in later chapters there may be several classes per source code file.

The example program writes Hello World! to the monitor. It looks like a lot of program for such a little task! But, usually, programs are much longer and the details you see here help to keep them organized. The line

public static void main ( String[] args )

shows where the program starts running. The word main means that this is the main method — where the Java virtual machine starts running the program. The main method must start with this line, and all of its parts must be present. Wherever there is one space it is OK to have any number of spaces. The spaces surrounding the parentheses are not required. The fifth line shows parentheses not surrounded by spaces (but you could put them in).

Question 4:

Is the following acceptable?

public    static void main(String[]  args   )