Java is an object-oriented programming language. The very first program that any programmer learns to code is the Hello World Program. So we will learn first a Hello World program. So that we can understand the basics. Because many a time we miss out on the nitty-gritty of the basic syntax. Through the medium of this article, We will get into the details of the Hello World Program in Java.
Topics which we will cover:
- How to write the Hello World program in java.
- Syntax Analysis to write basic java code.
- How to compile a java program.
- How to execute a java program.
Hello World program in Java-
Lets first understand with the coding and see how a basic Hello World program in Java is coded.
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
}
}
The above example shows how to write a Hello World program, let's analyze the program’s syntax in depth.
Syntax Analysis of Hello World program-
First Line: public class HelloWorld {
This line makes use of the keyword class for declaring a new class called HelloWorld. Since Java is an Object-Oriented Programming (OOP) Language, the entire class definition, including all of its members must be contained in between the opening curly brace { and the closing curly brace }. Also, it is using the public keyword to specify the accessibility of the class from outside the package.
Second Line: public static void main( String[] args ) {
It is called the main method(just like int main() in C ) and acts as the entry point for the Java compiler to begin the execution of the program. In other words, whenever any program is executed in Java, the main method is the first function to be invoked. Other functions in the application are then invoked from the main method. In a standard Java application, one main method is mandatory to trigger the execution.
public: it is an access modifier specifies the visibility. It allows JVM to execute the method from anywhere.
void: It represents the return type of the method. Since the main method in Java doesn’t return any value its return type is declared as void.
main(): It is the name of the method that has been configured in the JVM(Java Virtual Machine).
String[]: It represents that the Java main method can accept a single line argument of the type String array. This is also known as java command line arguments. Below I have listed down a number of valid java main method signatures:
public static void main(String args[])
public static void main(String… args)
static public void main(String[] args)
public static void main(String[] args)
public static void main(String []args)
public static final void main(String[] args)
final public static void main(String[] args)
public static void main(String[] name_arg)
Third Line: System.out.println( “Hello World” );
System: It is a pre-defined class in java.lang package which holds various useful methods and variables.
out: It is a static member field of type PrintStream.
println: It is a method of PrintStream class and is used for printing the argument that has been passed to the standard console and a newline. You can also use print() method instead of println().
How to compile a java program-
Now what you need to is type in this program in your text editor save it with the class name that you have used in your program. In my case, I will be saving it as HelloWorld.java
The next step is to, go to your console window and navigate to the directory where you have saved your program.
Now in order to compile the program type in the below command:
javac HelloWorld.java
Note: Java is case-sensitive, thus make sure that you type in the file name in the correct format.
If successfully executed, this command will generate a HelloWorld.class file which will be machine-independent and portable in nature.
How to execute a java program-
In order to execute your HelloWorld in Java program on the command line, all you need to do is type in the below code:
java HelloWorld
Note: You can choose any name for the file (. java). there is no matter to what is the name of classes in that file means that class names may be totally different from the file name. you should compile the program with the file name and you should run the program with the class name in which the main method exists.
1 Comments