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 :
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.