Structure of a Java Program
The program (or)
source code written in java is divided into 5 sections.
Namely,
1)
Documentation section
2)
Package statements
3)
Import statements
4)
Interface section
5)
Class section
Documentation Section: (optional)
This section
includes comments. It helps to improve the readability of the program. Comments
are non-executable statements. It is a simple message which states the purpose of
the program and exists only for the programmer.
These comments occur not just in the beginning but anywhere in the
program.
Package statements: (optional)
Java
allows us to group the required classes into one unit called package. This
section in a java program includes the declaration of a package. In a single
source file only one package declaration can be included.
Import statements:
Java has
many standard predefined classes that are stored into packages. An import
statement when declared refers to classes in other packages. The import
statements are given before any class definition. You can import a specific
class or all classes from the package. For example,
import java.util.*; /* imports all classes in util package */
import java.util.Date; /* imports only Date class in java.util package */
Interface section: (optional)
An interface is similar to a class but contains only declarations
and no definitions. They are implemented or extended by classes. They are used
to implement multiple inheritance concept of OOP’s .
Class section:
This section
gives the information about class, the user-defined data type , present in
program. It is a collection of data variables and methods(operating on data
variables). It consists of main() method where the execution starts.
The above
program is written in java to print “HelloWorld!” text.
Line 1: This is
the Documentation section. This line is a single-line comment
indicating the purpose of program
shortly called as statement solution.
Line 2: This is
import statement. Here, all the classes of java.util package are
Line 3: Line 3
to Line 9 come under Class section.
The word public is
a keyword (has a specific purpose)in java and is
called access specifier. The word
public indicates that the class is
accessible outside package.
The word static is
a keyword and allows the main() method to be
called even before the creation
of the object of the class.
The word void is a
keyword again and indicates that main() method
Declared does not return any
value.
The String args[]
is the one and only parameter of main() method of
String type. args[] is an array
of objects of the class that will be
created.
Line 7: Here,
the word System is the name of the standard class of IO devices
The word out
is the standard output stream and is the static
data member of the
class System.
The word println
is the method of out object which takes the
standard text as String
argument and displays it to the
standard output window
in a new line.
The curly braces, { only
indicate the beginning and ending of the function of class or any scope.
Comments
Post a Comment