Monday, June 28, 2010

First Java Program


Target audience: Beginners
Here, we are not going to talk about basics of java. The objective is to configure Java Development Kit (JDK) ,run a simple java program and understand the basics clearly.

Tools we need
1. JDK ( we are going to use version 1.5)
2. A Text Editior ( Notepad, TextPad,..)

What to be remembered before going into our game
Java is an object-orientated language
There are several types of Java programs and common are the stand-alone application, and the applet. Don't worry,you will get to know others later

First, we are going to write a basic program which just prints "Lets do this!". In Java, you write everything inside a Class. That' it. you can see the structure of a class bellow. A class should always be written inside open and end curly brackets


class Start {
}

Now i'm going to change this class to print "Lets do this!".As I stated earlier we need two tools JDK and a Text Editor. Open your text editor and write the following code.Save the code as Start.java in a prefered location ( i am going to refer the location as c:\myprogram, So the fulpath will be c:\myprogram\Start.java)


class Start {
public static void main(String[] args) {
System.out.println("Lets do this!");
}
}

what to be noticed:
The method "public static void main(String[] args)".This is the main method in java where your program starts running. Java will look for this method when you run a program. Java will do anything you write inside this method ( of course you have to follow some conventions and standards ). you do not need to worry about the method signature now as you are a very beginner. We'll cove all later. if you see the main method also be written inside open and end curly brackets. Yes,Methods also should be written inside open and end curly brackets.

To print a text ot String, you have to use System.out.println(SomeText) or System.out.print(SomeText) method. Here we used System.out.println("Lets do this!");. This will print a text "Lets do this!" in console (command prompt where you run the program). you will understand the details of the syntax later but for now, think that you say to java ( system) to print something in the output (console).

Now we will compile and run the program.
Hope you have downloaded the JDK and installed in a preferred place.( i am going to refer the JDK loaction as c:\tools\jdk1.5)
Go into JDK's 'bin' directory and check whether there are javac.exe( c:\tools\jdk1.5\bin\javac.exe) and java.exe( c:\tools\jdk1.5\bin\java.exe) file exixting. I'll explain what are they in a minute

How java runs the code we wrote.
Java turns the source code (eg: Start.java) to class file (Start.class). This is called compilation. Compilation is translating the source code (Start.java) into byte code ( Start.class). The Java tool which does the compilation is called Java compiler (javac.exe).What we get after the compilation is a byte code ( .class ). Java reads this byte code, understand and then run the application. This process is called interpretation. The java tool which does this process is Java Interpretor (java.exe)

Now we'll compile the code.
1.Open a command prompt ( Start->Run->type 'cmd' and perss 'OK')
2. Set JDK's bin directory in 'PATH' system environment variable
How to set: In command prompt type 'set PATH=c:\tools\jdk1.5\bin
Why we set:This will set the JDK's bin directory in systems environment variable. So whenever we type "javac" or "java", the system will go to "c:\tools\jdk1.5\bin" and run "javac.exe" or "java.exe" respectively
3. Once you set the PATH environment, the go to source directory in command prompt.
How to go: In command prompt type 'cd c:\myprogram". Then you can type "dir" to check whether you see your Start.java file
4. Compile the code
How to compile:In command prompt type "javac Start.java". If compilation is success, then you will see "Start.class" file in the same directory of source code file (c:\myprogram\Start.class)

Now we'll run the program.
How to run: Assume that you are still in the command prompt and the source code directory (c:\myprogram\). type" java Start". the you will see the out put "Lets do this!" in the command prompt console
Hope this helps to to understand the basics of compiling and running Java code or progarm.


Here is another video java tutorial for your reference



No comments:

Post a Comment