Structure of Java Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

STRUCTURE OF JAVA PROGRAM

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.

1. Documentation Section
It is used to improve the readability of the program. It consists of comments in Java which
include basic information such as the method’s usage or functionality to make it easier for the
programmer to understand it while reviewing or debugging the code. 
The compiler ignores these comments during the time of execution and is solely meant for
improving the readability of the Java program.
There are three types of comments that Java supports
 Single 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.
Eg:
// 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.
Eg:
/*calculate sum of two numbers
and it is a multiline comment */
 Documentation comments:
This comment style is new in Java. Such comments begin with delimiter /** and end with */.
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.
Eg:
/** The text enclosed here will be part of program documentation */
2. Package Statement
There is a provision in Java that allows you to declare your classes in a collection called
package. There can be only one package statement in a Java program and it has to be at the
beginning of the code before any class or interface declaration.
Eg:
package student;
This statement declares that all the classes and interfaces defined in this source file are a part
of the student package. And only one package can be declared in the source file.
3. Import Statement
Many predefined classes are stored in packages in Java, an import statement is used to refer
to the classes stored in other packages. An import statement is always written after the
package statement but it has to be before any class declaration.
We can import a specific class or classes in an import statement.
Eg:
import java.util.Date; //imports the date class
import java.applet.*; //imports all the classes from the java applet package
4. Interface Section
This section is used to specify an interface in Java. It is an optional section which is mainly
used to implement multiple inheritance in Java. An interface is a lot similar to a class in Java
but it contains only constants and method declarations.
Eg:
interface stack{
void push(int item);
void pop();
}
5. Class Definition
A Java program may contain several class definitions, classes are an essential part of any Java
program. It defines the information about the user-defined classes in a program.

A class is a collection of variables and methods that operate on the fields. Every program in
Java will have at least one class with the main method.

6. Main Method Class


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.


// Program to display message on the screen

class HelloJava
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
Sample Program
public class Example{
//main method declaration
public static void main(String[] args){
System.out.println("hello world");
}
}
public class Example
This creates a class called Example. You should make sure that the class name starts with a
capital letter, and the public word means it is accessible from any other classes.

Comments
To improve the readability, we can use comments to define a specific note or functionality of
methods, etc for the programmer.

Braces
The curly brackets are used to group all the commands together. To make sure that the
commands belong to a class or a method.

public static void main


When the main method is declared public, it means that it can be used outside of this class as
well.

The word static means that we want to access a method without making its objects. As we
call the main method without creating any objects.
The word void indicates that it does not return any value. The main is declared as void
because it does not return any value.

Main is the method, which is an essential part of any Java program.

String[] args
It is an array where each element is a string, which is named as args. If you run the Java code
through a console, you can pass the input parameter. The main() takes it as an input.
System.out.println();
The statement is used to print the output on the screen where the system is a predefined class,
out is an object of the PrintWriter class. The method println prints the text on the screen with
a new line. All Java statements end with a semicolon.

You might also like