Home Java Java-tips Examples-introductory Other First Program - Do Nothing

Ask Questions?

View Latest Questions

Advertisement


 
 

First Program - Do Nothing
Posted on: July 26, 2006 at 12:00 AM
Here is just about the smallest legal program you can write.

Prev: none | Next: Dialog Box Output

Java Notes

First Program - Do Nothing

Here is just about the smallest legal program you can write. It starts up, does nothing, and stops. Programs that actually do something will build on this basic structure.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
// Description: This is the smallest program.  It does NOTHING.
// File:   doNothing/FirstProgram.java
// Author: Michael Maus
// Date:   29 Jan 2005

public class FirstProgram {
    
    public static void main(String[] args) {
        // If the program did anything, it would go here.
    }
    
}
Lines 1-4 - Comments

Every program should have some identifying information at the front. This is information for the human reader -- comments are ignored by the compiler. This is some information I like to see: what the program is/does, the directory/file it's in (doNothng/FirstProgram.java), the author's name, and date. The exact formatting and other requirements vary, depending on your instructor or organization.

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

Line 5 - 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 6 - 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 "FirstProgram").
  • Class names begin, by convention, with an uppercase letter. As with other names, they may be continued with any number of letters (upper- and lowercase), 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 FirstProgram.java in this case) in a directory of its own. This isn't technically required, but as a practical matter it will save you some 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 8-10 - 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. You will notice that everything inside of braces for the class and main method is indented (space at the front). For every set of braces, the indentation should be increased one level. Typically each indentation level consists of 4 spaces.

Copyleft 2005 Fred Swartz MIT License
Advertisement


DMCA.com