First Java Program

Beginning Java programming with Hello World Example

Don't think to much just pick your notepad or any java editor tool start writing. It's quite simple. 

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 : predefined keyword "class" is used to declare classes in Java language.

public : It is an access specifier that is defined the scope. Public means that is visible to whole project.

static : static is again a keyword. It is very useful. You can study here (static keyword) how to use static. 

void :   It is the return type. You know everyone want some return. Void meaning this function will not return anything.

main : main() method This is the key point and most important method in a Java program. Java compiler first check the main method. This is the execution point of java program, Every java program execute from main method.

System.out.println : This is used to print anything on the console like printf in C language. You can see program result or any information output.


Steps to Compile and Run your first Java program :-

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

Step 2: Save the file as Hello.java or any file name.

Step 3: Just open command prompt and go to the directory where you saved/stored your first java program assuming it is saved in C:\ directory.

Step 4: Type  the command javac Hello.java 

javac - common name
Hello.java - File name

This command will call the Java Compiler asking it to compile the specified file. Compiler first check the error If there are no errors in the code the command prompt will take you to the next line. This command generate a .class file of your program. Name of the class file is your program name(Hello.class)

Step 5: Now if everything is ok just hit that command "java Hello" on command prompt to run your program. 

Step 6: Woow you will be able to see your program out "Hello world program" printed on your command prompt.


Now let us see What happens at Run-time:-

Now we move to next step what happen when our program is run. After writing your Java program, And you will try to compile it. Compiler will perform some compilation operation on your program.

Once Program is compiled successfully .class file is generated by the compiler.

When you will try to run the byte code(.class file), Some internally steps are performed at runtime:-

  1. First Class loader loads the java class. It is subsystem of JVM( Java Virtual machine).

  2. Now verifier checks byte code the code fragments for illegal codes that can violate access right to the object.

  3. Finally Interpreter reads the byte code stream and then executes the instructions, step by step.




 

 

Related Articles

post a comment