In this article, I have explained how you can create your first java program, using java "Hello World" program example. Simply by writing your first program code in notepad and then using the command prompt to compile it & show output.

Before we begin, you need to follow these steps, if you haven't done it yet.

  • Install the JDK
  • Set path of the JDK/bin directory

To set the path of JDK, you need to follow the following steps:

  • Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok

After completing the above procedure.

Creating First Hello World program in Java

In this example, we'll use Notepad. it is a simple editor included with the Windows Operating System. You can use a different text editor like NotePad++

Your first application, HelloWorld, will simply display the greeting " Hello World ". To create this program, you will to follow these steps:

  1. Open Notepad from Start menu by selecting Programs > Accessories > Notepad.
  2. Create a source file: A source file contains code, written in the Java programming language, that you and other programmers can understand.
    The easiest way to write a simple program is with a text editor.
    So, using the text editor of your choice, create a text file with the following text, and be sure to name the text file HelloWorld.java.
    Java programs are case sensitive, so if you type the code in yourself, pay particular attention to the capitalization.
    public class HelloWorld{
    
    	public static void main(String args[]){
    		System.out.println("Hello World");
    	}
    }
  3. Save the file as HelloWorld.java make sure to select file type as all files while saving the file in our working folder C:\workspace
  4. Open the command prompt. Go to Directory C:\workspace. Compile the code using the command,
    javac HelloWorld.java

    Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the JVM.

    The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
  5. Now type 'java HelloWorld' on command prompt to run your program
    run-java-first-program-using-command-prompt-min.png
    You will be able to see "Hello World" printed on your command prompt.

    Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret and run applets in any Web browser with built in JVM.Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program.

    The Java application launcher tool (java) uses the Java virtual machine to run your application.

    And then when you try to run the byte code(.class file), the following steps are performed at runtime:
    1. Class loader loads the java class. It is subsystem of JVM Java Virtual machine.
    2. Byte Code verifier checks the code fragments for illegal codes that can violate access right to the object.
    3. Interpreter reads the byte code stream and then executes the instructions, step by step.

Understanding the HelloWorld.java code (With few important points)

  • Any java source file can have multiple classes but they can have only one public class.
  • The java source file name should be same as public class name. That’s why the file name we saved our program was HelloWorld.java
  • class keyword is used to declare a class in java
  • public keyword is an access modifier which represents visibility, it means this function is visible to all.
  • static is a keyword, used to make a static method. The advantage of static method is that there is no need to create object to invoke the static method. The main() method here is called by JVM, without creating any object for class.
  • void is the return type of the method, it means this method will not return anything.
  • Main: main() method is the most important method in a Java program. It represents startup of the program.
  • String[] args is used for command line arguments.
  • System.out.println() is used to print statements on the console.
  • When we compile the code, it generates bytecode and saves it as Class_Name.class extension. If you look at the directory where we compiled the java file, you will notice a new file created HelloWorld.class
  • When we execute the class file, we don’t need to provide a full file name. We need to use only the public class name.
  • When we run the program using java command, it loads the class into JVM and looks for the main function in the class and runs it. The main function syntax should be same as specified in the program, else it won’t run and throw an exception as Exception in thread "main" java.lang.NoSuchMethodError: main.

You may also like:

Pyramid Triangle pattern programs in Java with explanation

Java program to reverse a string (Different ways explained)

Leap year program in Java (multiple ways)

Fibonacci series program in Java (With and without recursion)