Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How to Write Hello World Program in Java?

Last updated on Aug 23,2024 5.8K Views

Swatee Chand
Sr Research Analyst at Edureka. A techno freak who likes to explore... Sr Research Analyst at Edureka. A techno freak who likes to explore different technologies. Likes to follow the technology trends in market and write...
2 / 4 Blog from Environment Setup

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.

Below are the topics covered in this article:

Let’s get started.

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 HelloWorldDemo {
	public static void main(String[] args) {
		System.out.println( "Hello World!" );
        System.exit( 0 ); //success
	}
}

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

Syntax Analysis

Line 1: public class HelloWorldDemo {

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

Lets now break down this entire line and analyze each word:

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. 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)
  • public static void main(String args[])
  • public static void main(String… args)
  • static public void main(String[] args)  
  • public static final void main(String[] args)  
  • final public static void main(String[] args)  

Line 3: 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().

Line 4: System.exit( 0 ); 

The 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.

  • exit(0): It is used to indicate successful termination.
  • exit(1) or exit(-1) or any non-zero value: It is used to indicate unsuccessful termination.

So that was all about the program syntax. Let’s now see how to compile Hello World in Java program.

Compiling the 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  HelloWorldDemo.java. 

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 HelloWorldDemo.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 HelloWorldDemo.class file which will be machine independent and portable in nature.

Now that you have successfully compiled the program, let us try to execute our Hello World Program in Java and get the output.

Executing the 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 HelloWorldDemo

Voila! You have successfully executed your first program in Java.

In case you are using an IDE, you can skip all this hassle and just press the execute button in your IDE to compile and execute your Hello World in Program Java.

This brings us to the end of this article on Hello World Program in Java. If you want to know more about Java you can refer to our other Java Blogs.

Now that you have understood what is a Hello World Program in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

For more check out these Java interview questions for experienced.

Got a question for us? Please mention it in the comments section of this “Hello World Program in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Write Hello World Program in Java?

edureka.co