Structure for Java Program
A basic java program (Hello program) is divided into following parts:
import java.io.*; // Package
class Hello{ // class name and opening curly brace
public static void main(String args[]) // Main Method
{ // opening curly brace
// comment or /* comment */ // comment
System.out.println(“Hello World”); // statement
} //closing curly braces
} //closing curly braces
Explanation (structure for java program)-
- Package: Here the package is declared according to the program. Programmers will declare More than one package according to their need. Packages define classes, interfaces and sub-packages. These packages are declared at the top of the program.
- class name and opening curly brace: Here the name of the class is written with the keyword ‘class’. But the name of the class is not the keyword of java.
- public static void main(String args[])
- public: This is an access specifier. public means the method is accessed from any class.
- static: static means the class is accessed without object or instance.
- void: This main method allows nothing to be returned.
- (String args []): This is String, it is not a keyword, it is a class since its first character is in uppercase and args [] is an array of the object of the String class. This is the argument of the main function.
- Comment: This is a comment tag. It is not read by the compiler. It has two types.
- One Line Comment: It is commented with two slashes. // comment
- Multiple Line Comment: It is started with a slash and asterisk (/ *) and closed with asterisk and slash (* /). / * Multiple line comment * /
- out.println ()–
- System: This is a predefined class.
- out: This is the object of the PrintStream class, it is defined in the System class with the public static final these keywords.
- println: This is the method of PrintStream class. It is defined inside the PrintStream class with public. It is ‘ln’ with print. This allows the cursor to move to the new line.
- closing curly braces: here the main method and class is closed.