top of page
Writer's pictureCodersarts AI

Hello World Program in Java

The very first program that any Java programmer learns to code is Hello World Program in Java. But many a time we miss out on the nitty-gritty of the basic syntax. Through the medium of this article, I will get into the details of the Hello World Program in Java


Before we get into the details, lets first start with the coding and see how a basic Hello World program in Java is coded.


public class HelloWorldProgram{
	public static void main(String args[]){
		System.out.println("Hello world in java");
		System.exit(0);
	}
}

Now that you are done with the coding, lets now analyze the program’s syntax in depth.


Line 1: public class HelloWorldProgram {


This line makes use of the keyword class for declaring a new class called HelloWorldProgram . 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.


Line 2: public static void main( String[] args ) {


This line declares a method called main(String[]). It is called the main method 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.

static: It is a keyword which helps in making any class member static. The main method is made static as there is no need for creating an object to invoke the static methods in Java. Thus, JVM can invoke it without having to create an object which helps in saving the memory.

void: It represents the return type of the method. Since the Java main method 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.

String[]: It represents that the Java main method can accept a single line argument of the type String array.


Line 3: System.out.println("Hello world in java");


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().


Line 4: System.exit( 0 ); 


he java.lang.System.exit() method is used to exit the current program by terminating the currently executing Java Virtual Machine. This method takes a status code as input which is generally a non-zero value. It indicates in case any abnormal termination occurs.


Now in order to compile the program type in the below command:


javac HelloWorldProgram.java

In order to execute your HelloWorldProgram in Java program on the command line, all you need to do is type in the below code:


java HelloWorldProgram 

You have successfully executed your first program in Java.


Output:


Hello world in java



32 views0 comments

Comments


bottom of page