topLogoIntro
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Parts of a Java Program

Default Style from Eclipse:
EChelloGif
Alternate Style: Notice alternate positioning of curly braces:
/*************************************
 *Project:  LabOneA
 *Programmer:  John Smith
 *Date:  September 3
 *Program Name:  Hello.java
**************************************/

public class Hello
{
     public static void main (String[ ] args)
     {
            System.out.println("This is a literal print.");
            System.out.print("Yea!");
            System.out.println("Go Java!");
      }
}

It is possible to change the Eclipse default settings for the positioning of the curly braces:
go to
Window -- Preferences -- Code Style -- Formatter. Choose Edit. Choose tab for Braces.
Change the drop down boxes to "Next Line" for those locations you wish changed.


To avoid confusion, we will be using Eclipse with the default settings.

arrowblue
 Remember that Java is case sensitive.
The CODE: Information about the code:
/**********************************
 *Project:  LabOneA
 * @author John Smith
 *Date:  September 3
 *Program Name:  Hello.java
**********************************/
All programs should begin with a comment identifying the purpose of the program and the programmer.
/**documentation */ - documentation commenting.   
/* text*/ - is the format for block commenting.
// text - is the format for single line commenting.
Comment statements are ignored by the compiler.  It is a message to the reader of the code.
public class Hello
 
Every Java program is a class.  The program starts with the name of the class.  This name must be the same name as the . java file in your folder.  In this case the file is saved as Hello.java.

Class names must begin with a letter, an underscore or a dollar sign. 
Class names may contain only letters,
digits, underscores and/or dollar signs.
Class names may not use reserved words.

{ A set of French curly braces { } is needed for every class. As seen at the top of the page, the placement of the curly brace may be on the SAME line as public class Hello, or on a line by itself.
public static void main (String[ ] args) A main method is needed for execution to have a place to start.  This main method gets executed first.  The idea of static will be discussed later.  The array of strings parameter is a necessity.
{ A set of French curly braces { } is needed for main.
Again, placement may vary. Just be consistent.
System.out.println("This is a literal print.");
System.out.print("Yea!");
System.out.println("Go Java!");
Output statements:  the basic output statement in Java is the System.out.println( ) statement.
The System.out.println( ) line will print what is between the double quotes" " (called a literal print) and move the printing cursor to the next line.
The System.out.print( ) line will print what is between the double quotes and leave the printing cursor on the same line.
    }
}
The first } closes main method and the second closes the class Hello. They are always used to end the program.

The output from this program will be:

output1

Notice that there is no spacing between "Yea!" and "Go Java!".
A space was not specified, so no space appeared.

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.