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.

18 comments :

  1. great article. Thanks for sharing java Programming .its really helpful for me.java is the one of the most programming language build up on api....keep sharing on updated java tutorials?

    ReplyDelete
  2. I found best article.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete
  3. Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
    python course institute in bangalore
    python Course in bangalore
    python training institute in bangalore

    ReplyDelete
  4. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  5. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  6. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  7. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  8. Such a great information for blogger iam a professional blogger thanks…

    Upgrade your career Learn Oracle Training from industry experts gets complete hands on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  9. I really appreciate coming across your blog.

    موقع اسواق ديلي

    ReplyDelete