In this java tutorial, you will learn the essential program structure of the java programming language.
Program Structure
- Java has six sections of program structure. The details given as below
1. DOCUMENTATION SECTION
- It is an optional
- Gives the complete information about program like name of the program, author name, date, version & other details, etc
- Like c/c++, java supports two comment type statements
- Single Line Comment
- Multiline Comments
Single Line Comment
- It is used to ignore only one statement at a time
- It starts with operator like //
Example
Multiple Line Comments
- It is used to ignore more than one statements at a time
- This is done by using the operator like /* … */
Example
2. USER DEFINED PACKAGE SECTION
- Here we can create our own package
- Package is a set of classes / interfaces
- It is just like a container
Syntax
<package> <user-defined name>;
Example
NOTE
- It is important to note that class name and file name must be the same in java.
3. PREDEFINED PACKAGE SECTION
- It is an optional
- It is used to load the predefined java libraries using “import” keyword
- It is used to create different types of applications, algorithms, tools & OS, etc
- Ex. java.lang.*, java.io.*, java.util.*, etc
Syntax
<import> <package.subpackage.*>;
<import> <package.subpackage.Class>;
Example
import java.sql.*; // load all classes from sql package
import java.io.*; // load all classes from io package
import java.io.DataInputStream; // load only specific class
import
- It is a reserved keyword in java
- It is mainly used to load the system packages in java.
4. INTERFACES
- It has only declaration, no body (no definition)
- All the methods in the interfaces are public by default
- All the variables in the interfaces are const by default
- Interfaces are defined using interface (reserved word)
- It has no constructor
Syntax
< interface> <user-defined name>
{
// variable initialization
// method declaration, no definition
}
Example
NOTE
- If the subclass inherits the interface, then the sub class must implement the methods of interface, else subclass is also considered as an interface type.
5. CLASSES
- Collection of variables & methods are grouped together into a single entity (called class)
- classes are default or friendly modifier by default
- All the variables and methods in the class is default modifier by default
- Classes are defined using class (reserved word)
- It supports constructors (parameter & parameter less)
Syntax
Example
6. main() METHOD SECTION
- It is essential part in program
- It is an entry point of any java program
- It starts the program execution
- main() method accepts only void and int type. Other types are not allowed.
Example
NOTE
- It is an important to note that, main() method does not return any type except void (It does not return any value)
- Main() method must be void type.
OTHER TUTORIALS