Java Program Structure: A Java program consists of different sections. Some of them are mandatory but some are optional. The optional section can be excluded from the program depending upon the requirements of the programmer.
Documentation Section
It includes the comments to tell the program’s purpose. It improves the readability of the program.
Package Statement
It includes statement that provides a package declaration.
Import statements
It includes statements used for referring classes and interfaces that are declared in other packages.
Interface Section
It is similar to a class but only includes constants, method declaration.
Class Section
It describes information about user defines classes present in the program. Every Java program consists of at least one class definition. This class definition declares the main method. It is from where the execution of program actually starts.
1. Documentation Section: It includes the comments that improve the readability of the program. A comment is a non-executable statement that helps to read and understand a program especially when your programs get more complex. It is simply a message that exists only for the programmer and is ignored by the compiler. A good program should include comments that describe the purpose of the program, author name, date and time of program creation. This section is optional and comments may appear anywhere in the program.
Java programming language supports three types of comments.
Single line (or end-of line) comment: It starts with a double slash symbol (//) and terminates at the end of the current line. The compiler ignores everything from // to the end of the line. For example:
// Calculate sum of two numbers
Multiline Comment: Java programmer can use C/C++ comment style that begins with delimiter /* and ends with */. All the text written between the delimiter is ignored by the compiler. This style of comments can be used on part of a line, a whole line or more commonly to define multi-line comment. For example.
/*calculate sum of two numbers */
Comments cannot be nested. In other words, you cannot comment a line that already includes traditional comment. For example,
/* x = y /* initial value */ + z; */ is wrong.
Documentation comments: This comment style is new in Java. Such comments begin with delimiter /** and end with */. The compiler also ignores this type of comments just like it ignores comments that use / * and */. The main purpose of this type of comment is to automatically generate program documentation. The java doc tool reads these comments and uses them to prepare your program’s documentation in HTML format. For example.
/**The text enclosed here will be part of program documentation */
Package Statement: Java allows you to group classes in a collection known as package. A package statement includes a statement that provides a package declaration. It must appear as the first statement in the source code file before any class or interface declaration. This statement is optional. For example: Suppose you write the following package declaration as the first statement in the source code file.
package employee;
This statement declares that all classes and interfaces defined in this source file are part of the employee package. Only one package declaration can appear in the source file.
Import Statement: Java contains many predefined classes that are stored into packages. In order to refer these standard predefined classes in your program, you need to use fully qualified name (i.e. Packagename.className). But this is a very tedious task as one need to retype the package path name along with the classname. So a better alternative is to use an import statement.
An import statement is used for referring classes that are declared in other packages. The import statement is written after a package statement but before any class definition. You can import a specific class or all the classes of the package. For example : If you want to import Date class of java.util package using import statement then write
import java.util.Date;
This statement allows the programmer to use the simple classname Date rather than fully qualified classname java.util.Date in the code.
Unlike package statement, you can specify more than one import statement in your program.
For example:
Import java.util.Date; /* imports only the Date class in java.util package */
import java.applet.*; // imports all the classes in java applet
// package
Interface Section: In the interface section, we specify the interfaces. An interface is similar to a class but contains only constants and method declarations. Interfaces cannot be instantiated. They can only be implemented by classes or extended by other interfaces. It is an optional section and is used when we wish to implement multiple inheritance feature in the program.
interface stack {
void push(int item); // Insert item into stack
int pop(); // Delete an item from stack
}
Class Section: The Class section describes the information about user-defined classes present in the program. A class is a collection of fields (data variables) and methods that operate on the fields. Every program in Java consists of at least one class, the one that contains the main method. The main () method which is from where the execution of program actually starts and follow the statements in the order specified.
The main method can create objects, evaluate expressions, and invoke other methods and much more. On reaching the end of main, the program terminates and control passes back to the operating system.
The class section is mandatory.
After discussing the structure of programs in Java, we shall now discuss a program that displays a string Hello Java on the screen.
// Program to display message on the screen
class HelloJava {
public static void main(String args[]){
System.out.println(“Hello Java”);
}
}
Program’s Explanation:
1. The first line of the program begins with //, indicating that the remainder of the line is a comment. It describes the purpose of the program.
2. Since Java is an object-oriented language, so everything in a Java program resides inside a class. In our program, the second line
class FirstProg
defines a class named FirstProg. The keyword class before the class name identifies that it is a class definition. By convention, begin the class name with an uppercase letter and each subsequent word in a class name with an uppercase letter. The naming of the class must follow the same rules as for naming of any other identifier.
3. The opening curly brace ({) after the class name as in line 3 tells the compiler from where does the body of the class begin. The closing curly brace (}) in line 8 tells the compiler where does the body of class ends. Any code between these braces is considered to be part of FirstProg class. Note that lines between these braces are indented which is considered a good programming practice.
4. The lines 4 through 7 defines a method main () of the FirstProg class which serves as the entry point for the program execution. A method contains a collection of programming instructions that describe how to carry out a particular task. Line 4 specifies the main () method header which must always be written as
public static void main(String[] args)
We shall now discuss the various terms used in the method header,
• public: The public keyword is an access specifier which indicates that the method declared here should have public access, i.e. anyone can invoke it such as Java Virtual Machine (JVM). All Java programs must have at least one class that declares a public
method named main. The JVM will start the execution of the program at the first statement of the main method and terminates execution after the last statement.
• static: The keyword static allows main () to called before an object of the class has created. This is necessary since main () is called by the JVM before any objects are made. Since it is static, it can be invoked directly from a class.
• void: The type modifier void indicates that the main () method is declared does not return a value.
• string [] args: The argument String [] args is the only parameter in the main method. The String [] args declares a parameter named args which contains an array of objects of the class type String. The args receive any command-line arguments present when the program executed.
5. The code for the main method appears between the pair of curly braces. In our program, this method contains only one executable statement.
System.out.println(“Welcome to Java”);
This statement displays a message Welcome to Java in the command window.
This statement might look a little confusing the first time so to help make things clearer let’s examine the statement from left to right.
• System: It is the name of the standard class that contain objects that encapsulates the standard I/O devices for your system. It contained in the package java.lang. Since java.lang is imported in every Java program by default; therefore the java.lang is the
only package in the Java API that does not require an import declaration.
• out: The object out represents the standard output stream (i.e. command window) and is a static data member of the class System. To refer the out member of the System class write System.out.
• println(): The println() is the method of out object that takes the text string as an argument and displays it to the standard output, i.e. monitor’s screen. It then terminates the output line so that each call to println () displays its output on a new line.
A method is invoked by supplying an object reference (in this case System. out) and a method name (println() ) separated by the dot(.). It demonstrates one way in which you can call a class method.
The semicolon at the end of this statement specifies the end of the statement. Every statement in Java must end with a semicolon (;).
On executing this Java program, a message Welcome to Java would be displayed on the screen.
NOTE: In addition to println() method, System.out also has a print() method that displays a string but unlike println() method, the cursor does not move to the beginning of the next line after displaying the string.