Java Notes Prev: none | Next: Java Example: Dialog: Hello Earthling or Java Example: Console: Hello Earthling

# Java Example: Do Nothing

This is the smallest program you can write (except for comments). It starts up, runs but does nothing, and stops. Programs that actually do something all will build on this basic structure.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
    // Description: This is the smallest program.  It does NOTHING.
    // File  : examples-intro/examples1-sequential/DoNothing.java
    // Author: Michael Maus - 2007-03-27

    public class DoNothing {

        public static void main(String[] args) {
            //... If the program did anything, it would go here.
        }

    }

Lines 1-3 - Comments

  Every program should have identifying comments at the front. This information is for the human reader -- comments are ignored by the compiler. State what the program is/does, the directory/file it's in, your name, and date. The format will vary depending on your instructor or organization.

  Comments can be written in any of three styles (//, /*...*/, and /**...*/). I recommend the // style to start with. Everything from // to the end of the line is ignored by the compiler.

Lines 4, 6, and 10 - Whitespace (eg, a blank line, spaces)
  Insert blank lines to separate sections of your program. It's like starting a new paragraph in English. The compiler ignores them -- it's for us humans.
Line 5 - Class declaration
  You must define one or more classes to hold the parts of your program. Your class declaration should look like this, starting with either `public class` or just `class` (the difference at this point is irrelevant). Then you must name your class (here it is "DoNothing").
  - Class names begin, by convention, with an upper case letter. As with other names, they may be continued with any number of letters (upper- and lower case), digits, and underscores ("_").
  - Class names must be exactly the same as the name of the file that they are stored in -- except the file will have a "`.java`" extension.
  - Put all files related to this program (only the file DoNothing.java in this case) in a directory of its own. This isn't technically required, but as a practical matter it will save you pain later.

  Everything in the class is written between curly braces, `{}`. The left brace is written at the end of the class declaration line here, and the matching right brace that ends the class declaration is on line 12.

Lines 7-9 - Main method
  Every Java application starts in the `main` method. A method is a named group of instructions for doing something. You may define additional methods of your own, but you must write a main method with a first line that looks exactly like this. Like a class, the body of the method is enclosed between left and right braces. This body is empty. It does nothing.

  Indentation. Notice that everything inside braces for the class and main method is indented (spaces at the front). For every set of braces, the indentation should be increased one level. Typically each indentation level consists of 4 spaces.

  Brace convention. There are two popular brace formatting styles: put the opening brace on the end of the starting statement (K&R style) or put it at the beginning of the line below (Allman style). Both are fine, but don't mix styles in one program. My examples use K&R.

## Which style of input-output will you use?

There are two links at the top of the page to the next examples. The most common path is to use console output because this is used by most text books. The dialog output path is closer to what you'll do in a graphical user interface, but is used by fewer textbooks.