Hello World Program in Java

Swetha Ravindranath
Edureka
Published in
5 min readAug 7, 2019

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:

  • Hello World Program in Java
  • Syntax Analysis
  • Compiling the Program
  • Executing the Program

Let’s get started.

Hello World Program in Java

Before we get into the details, let's 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 us now break down this entire line and analyze each word:

public: it is an access modifier that 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 the Java program.

Compiling the Program

Now what you need to do 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.

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:

1

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.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, Python, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on August 7, 2019.

--

--