Simple Java Program
Java programs can be written in a very easy manner, follow the below two steps to create, compile and run the program.
1. Open a text editor, write the following SampleProgram program and save the file with .java extension i.e. SampleProgram.java
Note: The file name should be the same as the class name with extension .java - for example, if the class name is HelloWorld then the file name should be HelloWorld.java
Sample Java Program - SampleProgram.java
class SampleProgram {
public static void main(String[] args) {
// Declare and Initialize an integer variable 'age' with value 6
int age = 6;
// Print the local variable 'age' in the console window
System.out.println(age);
}
}
File Name: In Java, each file should be responsible for a single class and the name of the file should be a class name with an extension .java. In our example we named our class as SampleProgram so the file name should be SampleProgram.java
Identifiers: An Identifier is a name of a class, interface or a variable. The identifier should be unique within the package or the block scope level along with the hierarchy. In our example SampleProgram.java, we are having the following identifiers
- SampleProgram - Name of the class
- main - Name of a method
- args - Parameter of a main method
- String - Name of the predefined class (i.e. Its not a keyword)
- age - Name of the local variable
Keywords: A keyword is a reserved word by Java, so we can't use those words as an identifier and each keyword has its own functionality. In our SampleProgram.java example, we are having the following keywords
- class - Specifies the Class
- public - Specifies the access modifier
- static - Specifies the state of the method
- void - Specifies the return type of a method
- int - Specifies its an Integer data type
main method - Java program requires an entry point to start the execution. The main method main(String[] args) is the default entry point method to start the execution and the parameter args is an array of String data type in nature.
Note: Java program may have one or more class, but only one class will have a main method.
Statements - In our example we are having two statements in our main method such as
- int age = 6; - Its a Declaration Statement and initialized with value 6.
- System.out.println(age); - Its a method invoking statement, it invokes the predefined method println and prints the argument value into the console window (i.e. 6)
Semicolon - The basic rule in Java is the declaration, expression and invoking statements must end with a semicolon ;, not all the statements. It specifies the compiler that the statements gets end.
Block Scope { } - Open and close curly braces or curly brackets groups a set of statements and its called as Block scope
- Block scope in class class SampleProgram { } - The statements which is inside the curly braces or curly brackets are belongs to the specified class SampleProgram
- Block scope in method public static void main(String[] args) { } - The statements which is inside the curly braces or curly brackets are belongs to the specified method main