First Java Program

Let us look at a simple java program.

class Hello
  public static void main(String[] args) 
     System.out.println ("Hello World 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.

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

Steps to Compile and Run your first Java program

Step 1: Open a text editor and write the code as above.

Step 2: Save the file as

Step 3: Open command prompt and go to the directory where you saved your first java program assuming it is saved in C:\

Step 4: Type javac and press Return to compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line.

Step 5: Now type java Hello on command prompt to run your program.

Step 6: You will be able to see Hello world program printed on your command prompt.

Now let us see What happens at Runtime

After writing your Java program, when you will try to compile it. Compiler will perform some compilation operation on your program.

Once it is compiled successfully byte code(.class file) is generated by the compiler.

class-file at runtime in Java

After compiling when you will 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.