Friday, 8 March 2013

FIRST JAVA PROGRAM - HELLO WORLD!





In order to write your first Java Program, you require :
1. JDK installed on your computer.
    You can install it from here : 
    http://www.oracle.com/technetwork/java/javase/downloads/index.html

2. Setting of environmental variable path.

3. A simple text editor.

First Java Program - Hello World!

The code looks like this :



After writing the above code in notpad, save it anywhere with filename "Hello.java".
Note : All the java programs must be saved with the name equal to classname with an extension .java.

Now for compiling open the command prompt and go to the location of your file in which the java code was saved and type :

javac Hello.java

and press enter. 



If nothing happens it means the program is correct or not containing any error.
Now you need to Execute the program and for executing just type :

java Hello

and press enter



Now you can see that the output is "Hello World!" shown in the cmd.

EXPLANATION OF PROGRAM :

The code is shown as :


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

Since java is based on object oriented concepts, hence all the code must be stored in a class.
In above i am taking a class named "Hello" . You can use any name but make sure that the code is stored with the same name as that of the class . This is because when the code is compiled using java compiler then a class file (which contain byte codes)  is created with the same name as that of your file name.
And when you execute your program then the JVM (Java Virtual Machine) search for the class file and by using the name of class file it finds your program.
So, we can say that the name of file is same as that of the class name because JVM easily linked the class file with program file.

Now in the class there is a main method, which starts the execution of program.
In main method "public" is used as access specifier.
"static" is used because we want the main method should be run without making the objects of the class.
"void" is used because method is not return anything.
and parameters is passed as "String[] args" for handling the input given by user, which is not used in this program.

"System.out.println()" method is used for writing the output on the screen.

Read more

Wednesday, 6 March 2013

SETTING OF ENVIRONMENTAL VARIABLE PATH FOR JAVA



HOW TO SET THE ENVIRONMENTAL VARIABLE PATH FOR JAVA.



In Windows inorder to set

Step 1 : Right Click on  "My Computer"  and click on properties .




Step 2 : Click on Advanced tab




Step 3: Click on Environment Variables






Step 4: Now copy the path of your jdk/bin from the program files which lies in the folder named java in your computer where you install the jdk.
It is mostly stored in the C - drive.
Make sure that you choose jdk not jre.







Now copy the path as shown below..



Step 5: Now go to the Environment variables dialog box and create a new class path  
                 for JAVA_HOME.








Step 6 : Paste the path in the "variable value" column and set "variable name" as path.

After editing click OK.
Step 7 :Your are done setting up your environment variables for your Java , In order to test it go to command prompt and type
  javac



Now click "Enter" and If you get the output as shown below your path for java is set.


If you have any problem regarding this please comment below.

Read more