The Hello World Java program is used to print “Hello World!” on the screen. It is a simple program and hence used as a beginner’s guide to introducing Java programming language. Since we are talking about the beginning, let's start from scratch and learn about some basic requirements to write Java code.

What Are the Basic Requirements for Writing Your First Java Code?

The primary requirements to write a code in Java are following:

  • Installing the JDK
  • Setting the path to JDK/bin directory
  • Writing the program
  • Compiling the Java program
  • Running the program

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

How to Write Simple Codes in Java?

After installing the JDK, you can easily write and execute simple codes in Java. Considering that you have already downloaded and installed JDK, here’s how to write Java codes: 

1. Firstly open a notepad and write the code that you want to execute. In this example, we will use the ‘Java hello world code’. So open the notepad and type the code given in the image below:

HelloWorldJava_1

2. Next, save the file as Example.java. We are saving it as Example.java because our public class is Example. It means that your file name and public class should be identical.

HelloWorldJava_2

3. Now open your command prompt to start compiling and execution.

HelloWorldJava_3

4. After opening the command prompt, set the path of the JDK/bin directory. If you don’t set the path, the JVM compiler may throw an error “javac is not recognized as an internal or external command.”

HelloWorldJava_4.

5. At last, you have to compile and run the written code. Write javac Example.java to compile the code and java Example to run it and get the output.

HelloWorldJava_5

You can also use other Integrated Development Environments (IDEs) for executing Java files, but it is better to start with an easy procedure. Hence, we have used the command prompt here.

How to Write a Hello World Java Program?

Here’s how to write the Hello World Java program.

public class Example{  

     public static void main(String args[]){  

      System.out.println("Hello World!");  

}  

}

Output:

HelloWorldJava_6

Now since we know how to write the Hello World Java program, let’s learn about its parameters.

What Are the Parameters Used in Hello World Java Code?

In the above Hello World Java program, we have used the following parameters/keywords:

  • class: This keyword is used for class declaration in Java.
  • public: It is an access modifier keyword used to define visibility. Public means that it is visible to all.
  • static: This keyword declares a method as static. The most significant advantage of static methods is that they can save memory as object declaration is not required to invoke such methods.
  • void: The parameter void is used to define the return type. It means that the method will have no return type.
  • main: The main method is the starting point of a Java program that JVM looks for. Without the main method, the compiler will throw an error.
  • String args[]: It represents and stores command line arguments. The String args[] is an array of java.lang.String class in Java.
  • System.out.println(): This is the line of code that prints everything mentioned in the bracket. The System is a class, while out is an object, and println() is a pre-defined method of the PrintStream class.

Become a Certified UI UX Expert in Just 5 Months!

With The Best-in-class UI UX ProgramExplore Program
Become a Certified UI UX Expert in Just 5 Months!

What Are the Different Ways to Write a Java Program?

You can write a Java program in multiple ways, some of the ways are following:

  • Change Modifiers’ Sequence

You can write the main method as:

static public void main(String args[])  

  • Write Subscript Notation of an Array After the Type or Before or After the Variable

You can write the main method code also as:

public static void main(String[] args)

public static void main(String args[])

public static void main(String []args)

  • Provide var-args Support by Passing Three Ellipses in the Main Method

You can write it as:

public static void main(String... args)

  • A Semicolon After the Class End Is on You

Both the below hello world Java programs will give the same output:

public class Example{  

     public static void main(String args[]){  

      System.out.println("Hello World!");  

}  

}

Output:

HelloWorldJava_7

public class Example{  

     public static void main(String args[]){  

      System.out.println("Hello World!");  

}  

};

Output:

HelloWorldJava_8.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

In this hello world Java program article, you have learned to execute a basic Java program. Although hello world Java program is simple, it is essential to clarify various keywords and concepts of the Java programming language. If you are fond of programming, then Java is the easiest one to start with, you can learn the basics of Java by referring to Simplilearn's Free Java Course for Beginners. This course will help you to clear the basic concepts and lay a programming foundation. However, if you want to build the skillset in Java, you can apply for our Online Java Certification Course. It will assist you in learning advanced concepts and provide hands-on experience in multiple frameworks that will help you to build up your software development career.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449