Creating you First Java Application.


Hello again,here now we will deal with how to create a simple Java Hello World App which displays the message "Hello World!".To create this program, you must :

  • Create a Source file : The source file contains all the code of our program written in Java.You can generally use a text editor like Notepad for creating the source file.After writing the code just save it in the .java format.Example: Sample.java
  • Compilation of the Source file : Now the created source file(Sample.java) must be compiled to generate the ByteCode.It will be compiled by javac compiler which can only be understood by Java Virtual Machine(J.V.M).The command for compilation is javac Sample.java
  • Run the java file : The java application can be executed by J.V.M by using java command.The command for running the file is java Sample.

Creating a source file

To create a source file , just open the notepad and type the following code:

HelloWorldApp.java :
/**
 * The HelloWorldApp class implements an
 * application that simply prints
 * "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        // Display the string.
    }
}

Then, save the file as HelloWorldApp.java and exit NotePad.


Compilation of the Source File

Bring up the command prompt and navigate into the source file location.

Then,type the command
  • javac HelloWorldApp.java
Run the Java File
To run the Java File,after compilation you can see the output by typing the command.
  • java HelloWorldApp
Explaining HelloWorldApp
  • The HelloWorldApp source file contains Multiple line Comments which are useful in the development phase of the program.Its a good habit to Comment your programs as it is useful for others to know about your program.
  • HelloWorldApp class begins with the keyword class(A brief explanation will follow the next posts) which is embeded between the two curly braces " { } ".
class HelloWorldApp{ }
  • Main Method is the essential method for every class.It is the entry point into the program.It is named as "public static void main(String [ ] args)".The main reason behind this is:
-> Why main must be declared as public?
A). main() is invoked by JVM(i.e. J.V.M will access the main() method in our class)As JVM is outsider,it does not belong to our program package.So,inorder to access main outside our program we declare it as public.Or else we will get a compilation error known as main method not public.
-> Why main must be declared as static?
A).If a method is declared as static in the class we can call it with using classname.methodName(); So in order to call the main method it must be static.
-> Why main must be declared as void?
A). A void keyword means it returns nothing.JVM expects nothing when calling the main method.So it is named as void.
This sums up the introduction to the basic Java Program.
This is the introductory Hello World App.Stay Tuned for more Tutorials!

Au Revoir!!