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.

Back from Arizona

Well, almost 4 days since I'm back, actually....so this post is a tad late.....
anyways...better late than never

As for the trip, I was quite apprehensive initially, but I guess it was really uncalled for...... made some really nice friends...... and met a most wonderful person..... We had great fun, watched so many movies, biked around, lazed around, let our hair down.........

A most amazing and refreshing break I would say. One that I never wanted to end....

Coming back is always tough... coming back to work...starting up again....leaving back memories of time well spent....

But then one must always come back..... and stay happy.....For life is just a collection of memories, both happy and sad....and its always better to focus on the happy to overcome the sad....

Actually the new year brings in a new start for me.... to overcome my inconsistencies, my shortcomings, to exceed and excel, to assert myself and achieve my true potential...to fulfill my dreams....and its been good I guess till now....

Should old acquaintance be forgot,
and never brought to mind ?
Should old acquaintance be forgot,
and auld lang syne ?
CHORUS:
For auld lang syne, my dear,
for auld lang syne,
we'll take a cup o’ kindness yet,
for auld lang syne.

And surely you’ll buy your pint cup !
And surely I’ll buy mine !
And we'll take a cup o’ kindness yet,
for auld lang syne.
CHORUS
We two have run about the hills,
and pulled the daisies fine ;
But we’ve wandered many a weary foot,
since auld lang syne.
CHORUS
We two have paddled in the stream,
from morning sun till dine (dinner time) ;
But seas between us broad have roared
since auld lang syne.
CHORUS
And there’s a hand my trusty friend !
And give us a hand o’ thine !
And we’ll take a right good-will draught,
for auld lang syne.
CHORUS



Happy New Year
and God Bless [:)]

meebo