Sunday, January 7, 2007

Believing in Java -1

My most long awaited post series is finally here....
actually i don't know if many people were waiting for this :P
at least I was :)

So how do I start?

or rather how do you start?

If you are here then you must be:
1)a software junkie/nerd wanting to start with Java, but did not know where to start.
2)someone who knows the syntax, but never actually tried writing any code
3)a C++ programmer who finally realised that it sucked (actually just joking)
4)A forgetful 25 year old graduate student who must do this, so that he can immortalise his knowledge (provided blogger.com will survive the next ice age)


i am not explicitly going to teach OOP here....so if u are here for OOP purely...you must google something else....altho if you are patient....and iff i do not forget...there will be some examples

Let's start at the very beginning, a very good place to start....


Java is write once run anywhere...which means that the only requirement to run a java app on ur computer is to have a Java Virtual Machine installed on it. That's it. It doesn't matter what kinda hardware/OS u have, just make sure u have a JVM and u r good to go. I guess this is of the most powerful features of Java, a feature that makes it portable...the other stuff that i really like is:
1)Me no understand pointer speak.....(actually this is not very true)...but java does not require you to explicitly manage memory, which means on the upside, extremely programmer friendly...but on the downside could cause memory leaks...and is also considered by it's critics to be slow. Garbage collector settings are tweakable at the system level.
2)Extensible and hence infinitely powerful.
3)Relatively simple to program in.


To get a lowdown on other stuff....you must check out the wiki on Java (Programming Language)

However just having the JVM, which is part of the Java Runtime Environment (JRE) does not allow you to develop applications in Java. You also need a Developer's Kit..... a JDK. Both the runtime and the JDK are freely downloadable here

You may also download an IDE. IDEs make life really simple for a programmer, because of the on-the-fly compile feature among others. But its always better to start learning the harder way, using command line...that's how I started. 2 IDEs I believe in are Netbeans and Eclipse. Remember an IDE is just a development environment. It may not necessarily have the runtimes and other tools that come with a JDK.

Your first Java Program

Yes this is it, the Hello World you've all been waiting for


public class Hello
{
    public static void main(String args[])
    {
        System.out.println("Helo World");
    }
}

Wow so much code, but what do we do with it.

Step 1:

Using a text editor, (Word pad or notepad or editplus are pretty good editors if you do not have an IDE/don't know how to use one), create a file Hello.java. The default extension in Notepad is .txt. So don't create a file Hello.java.txt....instead change it to all files (*.*) and save as Hello.java. Copy paste the above code into the file and save. The source contains one class named Hello. Even if you might be deviating from my instructions, please ensure that the source code file name must be the same as the class.

Step 2:

U have the source file now. All that remains is to compile it. To compile a source all we need to do is to pass the file name with location to the javac tool. This and several other useful tools that are bundled with the JDK are available in the /bin folder. So a good idea would be to add this location to the PATH environmental variable.

also add this to the CLASSPATH variable

/lib/tools.jar.

In case you do not have a classpath variable create one and add this.

To add an entry is equivalent to appending to the existing text a semi colon and the new entry.

To compile type

javac /Hello.java

if you followed everything correctly uptil now, the command terminates succesfully and the prompt is on a new line.

There could be 3 kinds of errors.
1)A compilation error if you did not copy paste the code correctly or mistyped some syntax. Or you did not add the tools.jar entry. In case you cannot modify the classpath variable add a -classpath flag and provide the tools.jar entry after it.
2)A file not found error if the source code file does not exist, or the location specified is incorrect.
3)A command not found error, in case you did not add the bin location in PATH. In case you can't change the environmental variable don't worry. Specify the complete path for the javac command.

The class should be created at the location you are currently running the command.

Step 3:
To run the class type
java Hello.

If you get a similar error as 1 or 3 in Step 2, do similar work arounds.

You should have been able to see a Hello World.

Congratulations....you've just written your first Java application. Now rest for a while and wait for my next post.

No comments:

meebo