Creating a Hello World! program is not a oneline code but it contains various parts.Since java is an Object Orient Programming Language (OOPL) it should be written within the class.

A sample Java Code is here!

package javaBasics;

public class MyFirstProgram
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}
//Output:Hello World!

Let’s look at the parts of our first program

class : class keyword is used to declare classes in Java

public : It is an access specifier. Public means this function is visible to all.

static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.

void : It is the return type, meaning this function will not return anything.

main : main() method is the most important method in a Java program. This is the method which is executed, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error.

String[] args : This represents an array whose type is String and name is args. We will discuss more about array in Java Array section.

System.out.println : This is used to print anything on the console like printf in C language.


Steps to Compile and Run Java Program

Step 1: Open a text editor or IDE like notepad, sublime, eclipse, or intellij write the code as above

Step 2: Save the file as anyname.java {anyname means any file name}

Step 3:

  • if using any text editor

Open the command prompt and go to the directory{google it if don’t know} where you saved your first java program, assuming it is saved in C drive.

Write this command

javac anyfilename.java Then press enter.

java anyfilename {if no error}

  • If using any IDE it means just press the RUN icon.

Step 4: If there is no error the program will execute.

Step 5: You will be able to see Hello world printed on your command prompt or console.


Flowchart of the execution process

Steps performed at runtime
  1. Classloader loads the java class. It is a subsystem of the JVM Java Virtual machine.
  2. Bytecode verifier checks the code fragments for illegal codes that can violate access right to the object.
  3. The interpreter reads the bytecode stream and then executes the instructions in a step by step manner.