This page was saved using WebZIP 6.0.8.918 (Unregistered) on 01/20/05 오후 3:27:46.
Address: http://www.leepoint.net/notes-java/examples-introductory/other/program1.html
Title: Java: First Program  •  Size: 4156  •  Last Modified: Fri, 14 Jan 2005 00:25:32 GMT

Java: First Program

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. Below is an explanation of the basic parts.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
// FirstProgram.java
// Michael Maus, 25 August 2004
// This is the smallest program.  It does NOTHING.

public class FirstProgram {
    public static void main(String[] args) {
    }
}
Lines 1-3 - Comments

Every program you write should have some identifying information at the front. This is information for the human reader -- comments are ignored by the compiler. As your instructor, I want to see the name of the program or file (FirstProgram.java) on the first line. You must include your name and the date. And finally, a (at least) one-line description should be included.

Comments can be written in any of three styles (//, /*...*/, and /**...*/). I recommend the // style because it's most commonly used. Everything from the // to the end of the line is ignored by the compiler.

Line 4 - Whitespace
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.

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 8.

Lines 6-7 - 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 like this. Like a class, the body of the method is enclosed between left and right braces. This body is empty. It does nothing.