Documentation
Section
|
/*
This is a sample java program
Save this file as Welcome.java
*/
|
Package Statement
|
package sample;
|
Import statements
|
import java.lang.*;
|
Class Definition
|
class SampleProgram
{
|
Comment Section :
|
// A java program will start from here.
|
Main Function
|
public static void main(String args[])
{
System.out.println(" Welcome to Suman Blog!!! ");
}
}
|
- Package Statement
- It is package declaration statement.
- The package statement defines a namespace in which classes are stored.
- The package is used to organize the classes based on functionality.
- If you omit the package statement, the class names are put into the default package, which has no name.
- Package statement cannot appear anywhere in the program. It must be the first line of your program or you can omit it.
- Import statements
- It includes statements used for referring classes and interfaces that are declared in other packages.
- public class JavaProgram: This line of code has three parts:
- public : This is an access modifier keyword, tells the compiler to access the class. Various values of access modifiers are public, protected, private or default (no value).
- class : This keyword is used to declare the class. Name of the class (here JavaProgram) followed by this keyword.
- JavaProgram : This is the name of the class. You can give this name according to your demand/program.
- Comment Section : You can write comments in two ways:
- Single Line Comments : It start with two forward slashes i.e. // and continue to the end of the current line. Line comments do not require an ending symbol.
- Multi Line Comments : Also called as block comments start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/).Block comments can also extend across as many lines as needed.
- public static void main (String args[]) : This line of code has three keywords, main, and string arguments:
- public : Access Modifier
- static : static is reserved keyword which means that a method is accessible and usable even though no objects of the class exist.
- void : This keyword declares nothing would be returned from method. Method can return any primitive or object
- main : This is the main where program starts running or from here our compiler starts checking and running our java program/code.
- Arguments : This is for the arguments purposes which will be included inside the curly bracket.
- System.out.println("Hello World"): This line of code has also four parts:
- System : It is the name of Java utility class.
- out : It is an object which belongs to System class used in sending the data which is inside the bracket to the console.
- println : It is print line which is utility method name which is used to send any String to console.
- Hello World : It is String literal set as argument to println method