Java Hello World – Create Your First Program In Java Today

This Tutorial Explains How to Write Your First Program – Hello World in Java. It includes Creation, Compilation, and Execution of the program:

In this tutorial, we will write and discuss the various components of the first program in Java, i.e. the universal program “Hello, World”.

Simple examples have been included in this tutorial for your easy understanding of the concept.

Let’s start!!

Java Hello World - First Java ProgramJava Hello World - First Java Program

How To Begin Java Programming?

The basic steps to begin Java programming are as follows:

  1. Install the latest Java version.
  2. Set the appropriate path variables.
  3. Create the Java program.
  4. Compile and execute the program.

So far we have seen the Java tutorials related to Java download, installation, setting up IDE, etc., we have also discussed two popular Java IDEs i.e. Eclipse and IntelliJ IDEA. Next, we discussed the features introduced in Java 8 that was the most important release.

Now after following these tutorials, you have set the latest Java version on your machine along with all the required environment variables, and you have got Java working on your machine. Thus it’s now the time to start with actual Java programming and learn its various programming constructs i.e. in this tutorial we will learn steps 3 & 4 listed above.

Let us begin with the universal program that is always the first program irrespective of which programming language you are learning. The “Hello, World” program.

We will first learn how to develop a Java program in a step by step manner and then compile and execute it using the command prompt. We will also see how to run the same program in a Java IDE.

Steps To Write A Simple Java Program

#1) Open notepad and type the following code.

public class myfirstclass{
public static void main(String[] args)
{
            System.out.println(“Hello,World”);
}
}

#2) Save the above file as “myfirstclass.java” in your code directory.

myfirstclass.javamyfirstclass.java

As the class “myfirstclass” is declared public, you will name the Java file with this class name. This is a Java convention.

#3) Open the command prompt and go to the directory where you just saved your Java file.

#4) Next type in the command “javac myfirstclass.java” in the command prompt. The following output is displayed. Note that there are no errors and the file is compiled successfully.

Successful_CompilationSuccessful_Compilation

#5) Check the folder containing Java file. You will see that a class file “myfirstclass.class” is generated as a result of the compilation. Note that irrespective of what name you give to your Java source file, a class file contains the name of the actual class.

Class _FileClass _File

#6) If there is more than one class in the source Java file, then a class file is created for each file.

#7) Next, go back to the command prompt and type the command “java myfirstclass”. This will execute the class file and output displayed.

Execute the Class FileExecute the Class File

Alternatively, if you want to use IDE like Eclipse for creating Java programs, then you can create a project and add a class with the main function. You can give a print statement to print “Hello, World” and then run this project as a Java application as shown in the below image.

Eclipse IDE ExecutedEclipse IDE Executed

Explanation Of “Hello, World” Java Program

We have already developed and executed the “Hello, World” Java program successfully. Now let us analyze and understand each component of the program.

For your convenience, the contents of the program are posted below.

public class myfirstclass{
        public static void main(String[] args)
        {
                 System.out.println(“Hello, World”);
        }
        }

Let us understand the program by listing the main Java keywords that are used:

  • Public: This is a visibility specifier or access specifier that defines the visibility of the component. Public means the parameter or component is visible to all. In the above program, we use a “public” specifier twice i.e. first before the class definition and second for the main function. This means the class, as well as the main function, are public and visible to all.
  • Class: This is a Java keyword and is used to define a class. The “class” keyword is followed by a class name that defines the class. After the class name, a code block is defined with curly braces ({}). The code block will contain all the methods and data belonging to this class. Here we define a new class “myfirstclass” using the class keyword.
  • Static: The keyword “static” is used to indicate that the method/object/variable that follows this keyword is static in nature and it can be invoked without using the object and the dot (.) operator. The static keyword before the main method means that the main method is static. The Main method is executed by JVM and by making it static there is no need to create an object thereby saving the memory.
  • Void: The “void” keyword indicates that the method does not return anything.
  • Main: The keyword “main” that indicates the main method is the starting point of any Java program. The execution of a Java program begins with the main method.
  • String[] args: The string array args holds command line arguments.
  • System.out.println: System.out.println is used to print messages to the screen. The system is a class in Java. The parameters “Out” and “println” are the members of the PrintStream class. While “out” is an object, println is a method.

Frequently Asked Questions

Q #1) What is Hello World in programming?

Answer: “Hello, World” is a simple program that displays the message “Hello, World”. This is a general computer program that demonstrates the basic syntax of the programming language being used. Developing this program is quite simple and is usually the first program written when a programmer learns to code.

Q #2) Who started Hello World?

Answer: Brian Kernighan, the author of the book “C Programming Language” first referenced the phrase “Hello, World” in his book and from then it has been used universally to illustrate the basic syntax or the first program in most of the programming languages.

Q #3) What is a simple Java program?

Answer: A simple Java program is good to assess the programmer’s basic skills and also learn the basic syntax of the Java programming language.

You can start with a simple “Hello, World” program that demonstrates the use of class, static, public and void keywords, main function, command-line arguments, System.out.println, etc.

This tutorial describes a simple Java program “Hello, World”.

Q #4) Where can I write Java code?

Answer: You can write Java code in simple a notepad or notepad++ text editor and then compile and execute it using the command prompt. Alternatively, there are a lot of Java IDEs like Eclipse, Net Beans, IntelliJ IDEA, etc. that have the Java environment ready to develop the simple program as well as bigger, complex projects.

The following are the basic steps to create a simple program in Java.

  • Write the Java Source Code in any text editor.
  • Save the File with an extension “.java”.
  • Open a command prompt window.
  • Change the Directory to the one which has the Java program just created.
  • Compile your program using the “javac” command followed by the Java filename.
  • Execute the Program using the “java” command followed by the class name.

Q #5) What is the main class in Java?

Answer: The main class of Java is the one that contains the “main” method. As per Java convention, at least one class should contain the main method that happens to be the entry point for the Java program.

Q #6) How do you say Hello World in Java?

Answer: Steps to say “Hello, World” in Java are given below:

  • Create a class HelloWorld.
  • Declare the main method in the class “HelloWorld”.
  • Output the message “Hello, World” using System.out.println.
  • Compile and execute the program to see the “Hello, World” message.

Conclusion

In this tutorial, we developed the “Hello, World” program which was the first Java program. We also discussed all the parts of this program in detail.

In our next article, we will learn more about the terms and definitions in Java language along with the basic syntax and programming of the Java language.

Happy Learning!!