Monday 6 July 2015

Java Sample Program

In this post, I will tell you about how to work with Java.

Getting Started - First Program of Java

You should have already installed Java Development Kit (JDK). Otherwise download it and install JDK to your system. 

Let's start by writing our first Console application that prints a message "Hello World".

  • Write the Source Code: Enter the following source codes using a programming text editor (such as Notepad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for Ubuntu); or an Interactive Development Environment (IDE) (such as Eclipse or NetBeans).
Source Code :

class PrintMessage
{
                     public static void main(String args[])
                     {
                                      System.out.println("Hello World!!!");
                      }
}
  • Compile the Source Code: Compile the source code "Hello.java" into portable bytecode "Hello.class" using JDK Compiler "javac". Start a CMD Shell (Windows) or Terminal (UNIX/Linux/Mac OS X) and issue this command:
shell > javac PrintMessage.java
There is no need to explicitly compile the source code under IDEs (such as Eclipse or NetBeans), as they perform incremental compilation implicitly.
  • Run the Program: Run the program using Java Runtime "java", by issuing this command:
shell > java PrintMessage
Hello World!!!