Sunday, February 8, 2015

Your First Java Program: Hello World




In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. The Java system is a collection of applications not unlike any of the other applications that you are accustomed to using  browsers etc. As with any application, you need to be sure that Java is properly installed on your computer. You also need an editor (notepad , wordpad etc) and a terminal application(command prompt etc). 

Programming in Java. We break the process of programming in Java into three steps:

1. Create the program by typing it into a text editor and saving it to a file named,  say, helloworld.java.
2. Compile it by typing "javac helloworld.java" in the command prompt window.
3. Run (or execute) it by typing "java helloworld " in the command prompt window.

The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named helloworld.class);the third actually runs the program.
1.      Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. helloworld.java is an example program. Type these character into your text editor and save it into a file named helloworld.java.




Remember that the file name should be same as the program class name.


2.      Compiling a Java program. At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, to the contrary, the language is designed to be best understood by the programmer (that's you). A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version).


Open cmd.exe window.







Give a path to that drive where you have stored your helloprogram.java file (mine is M drive) 






Create a folder java in your drive and move the .java file in that folder and open the folder in cmd window (cd command is used to open a folder).



Now open the program file by writing “javac helloworld.java” , this will create a class file




 











3.      Executing a Java program. Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the helloworld program, type the following at the cmd window:
               “     java helloworld      ”



OUTPUT






Also feel free to ask any doubts regarding the projects.







-Manish :)

No comments:

Post a Comment