Java Notes Prev: none | Next: Dialog Box Output
First Program - Do Nothing
This 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 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.
- 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 classor justclass(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 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
mainmethod. 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.