Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

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.

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]