Friday, 31 January 2014

Running Simple Hello World Applcation in Java

                                       Installation and setting up java

Installation :

   You can download the java software from the below location

      http://www.oracle.com/technetwork/java/javase/downloads/index.html


1. Install the product.

2. Setting up the java environment

2.a On Windows

   Suppose you install the java in the below directory,

       C:\Program Files\Java

   Then java compiler (javac) and java launcher (java) will present in

        C:\Program Files\Java\jdk1.7.0_13\bin

  We need to add the “C:\Program Files\Java\jdk1.7.0_13\bin” to system path, then we can run our java programs from any where

Follow the below steps

Step1 : Right click computer icon, and open the properties window.

Step 2:Click on the Advanced system settings tab as shown below. Then it will open the system properties window.



Step3: Open the System properties and choose the Advanced tab. Click on the “Environment Variable” button.


Step 4
You will get a window like below. It contains 2 sections, one for user variables and other for System variables section.


Edit the path from system variables section

Append the java bin path to the value filed of System variable "path".

Before appending prepend a semicolon like below

;C:\Program Files\Java\jdk1.7.0_13\bin


 
Press Ok.

How to check whether you set the path properly or not

Open command prompt and type javac. If you see the output like below. You successfully set up the java environment.


On Open Systems

If you install the java on below directory “/usr/java7”

Then java compiler (javac) and java launcher (java) present in the directory “/usr/java7/bin”

You have to update the path like below

      export PATH=/usr/java7/bin/:$PATH

              OR

     you update the path variable in /etc/environment file for linux or .profile file in AIX like below

       export PATH=/usr/java7/bin/:$PATH


Hello World Application
 
 Lets start by simple program usually printing "Hello World".

Open notepad, and type the below code

class Hello{
    public static void main(String args[]){       
        System.out.println("Hello World");
    }
}
Save the file as Hello.java

Let the file saved in location "C:\java Programs". Open command prompt and goto the directory where the file is saved.




How to compile the Program

   Use the below command

      javac Hello.java

How to run the Program

   Use the below Command

      java Hello

After you run the program with the command "java Hello" You will see the output "Hello World" on the console

Discussion:

1. File name should be exactly equal with your class Name. Here class name is "Hello", so i saved the file name as Hello.java.

Close Look at the Hello World program

--------------------------------------

Hello World Program has mainly 3 Components.

1. Class Definition

2. The Main Method

3. println method


1. Class Definition

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

Java is Object Oriented language. So Everything you write must be enclosed in the class declaration.

Basic Syntax of class is

class Class_Name{

//Statements

}
By convention, a class Name always starts with Capital letter followed by small letters like Abc, Hello, and each subsequent word in the class declaration also starts with capital letter like "ThreadExample"

2. The Main Method
class Hello{
    public static void main(String args[]){
      System.out.println("Hello World");
   }
}

In the Java programming language, every application must contain a main method whose signature is:


Don't bother about the signature of the main method, you will learn later. Now remember, Program execution always starts from main method.

3. println method
it simply prints the information whatever you given to it, to the terminal or console.




Features Removed from C and C++                                                 Data Types in Java                                                 Home

No comments:

Post a Comment