Thursday, February 4, 2010

java-1.2

It’s traditional to start learning a new programming language by writing a program called "Hello World". You can think of it as a very simple initiation into the ranks of Java programmers. All the program does is write the text "Hello World!" to your computer screen.

The basic steps we will follow for our Hello World program are:
   1. Write the program in Java
   2. Compile the source code
   3. Run the program

Creating Your First Java Program


Write the Java Source Code

Program Code in Notepad

The first thing you need is a place to write your Java code. All Java programs are written using plain text files, therefore you can write them without any fancy software. For your first program, open up the simplest text editor you have on your computer. I’ll be using Notepad.
The program code looks like this:
// The classic Hello World program!

class HelloWorld {

   public static void main(String[] args) {

      //Write Hello World to the terminal window
      System.out.println("Hello World!");

   }
}
You could cut and paste the above code into your text editor, but it’s better to get into the habit of typing it in. It will help you learn Java quicker because you will get a feel for how programs are written, and you will make mistakes! It may sound odd, but any mistakes you make will help you become a better programmer in the long run. Just remember that your program code must match the example code, and you’ll be fine.
For this first program, I’m not going to explain how it works or what all the code means; that will come in later tutorials. At the moment, it’s more important for you to experience the steps you should follow to create and run a program.

Save the File

Save the File

Save your program file as "HelloWorld.java". I’ve created a directory in My Documents called "Java" to store all the example Java programs in one place. It makes it easy for me to find them when I need them. You might consider creating a similar directory on your computer.
It’s very important that you save the text file as "HelloWorld.java". Java is a little bit picky about filenames. If you look at the code you will see the statement:
class HelloWorld{
It’s an instruction to call the class "HelloWorld". The filename must match this class name, hence the name "HelloWorld.java". The extension of ".java" tells the computer that it’s a Java code file.

Open a Terminal Window

Run Dialog Box

Most programs that you run on your computer are windowed applications; they work inside a window that you can move around on your desktop. The HelloWorld program is an example of a console program. It does not run in its own window; it has to be run through a terminal window instead. A terminal window is just another way of running programs stored on your hard drive.
To open a terminal window, press the "Windows key" and the letter “R”.
You will see the "Run Dialog Box". Type "cmd", and press "OK".
A terminal window will appear on your screen. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files they contain, and run programs. This is all done by typing commands into the window.

The Java Compiler

Set the Compiler Path

Another example of a console program is the Java compiler called "javac". This is the program that will read the code in the HelloWorld.java file, and translate it into a language your computer can understand. This process is called compiling. Every Java program you write will have to be compiled before it can be run.
To run javac from the terminal window, you first need to tell your computer where it is. On my machine it’s in a directory called "C:\Program Files\Java\jdk\1.6.0_06\bin". If you don’t have this directory, then do a file search in Windows Explorer for "javac" to find out where it lives.
Once you’ve found its location, type the following command into the terminal window:
set path= *the directory where javac lives*
E.g.,
set path=C:\Program Files\Java\jdk\1.6.0_06\bin
Press Enter. The terminal window won’t do anything flashy, in fact it will just return to the command prompt. However, the path to the compiler has now been set.

No comments:

Post a Comment