Conor Mac Aoidh
http://macaoidh.name
conor@macaoidh.name
 
« Twitter Weekly Updates for 2009-04-20
Twitter Weekly Updates for 2009-04-27 »

Hello World – Java

Posted Apr 26th, 2009 by Conor

I wrote my first Java application today. Ir is traditional to start out with something simple and boy this is simple and it works!

First you need to download and install the Java Development Kit (JDK) from Sun here. Then change directory to your workspace – where ever you want! In my case it was:

cd /home/conor/java

Then open up a new file named HelloWorld.java in your favourite text editor. In my case it was:

vim HelloWorld.java

Now we start the coding:

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

This is basically just a class that tells Java to output the words ‘Hello World! Now the code must be turned into machine code so that it is faster to run. To do that simply type:

javac HelloWorld.java

This creates a new file named HelloWorld.class which is written in machine code. If you open this file in your text editor all you will see is a load of gobledeegook. Now the application is ready to run. Type the following command to run it:

java HelloWorld

You should then see Hello World! written on your screen like this:

[conor@host java]$ java HelloWorld
Hello World!

Another one in the bag!

3 Responses to “Hello World – Java”

  • david on 26 Apr 2009 at 10:51 pm

    // c version

    #include “stdio.h”

    void main() {
    printf(“hello world!”);
    }

  • Conor on 26 Apr 2009 at 11:01 pm

    Seems like Java and C have very similar syntax. I plan to learn C but I am using Java as an introduction to non web based programing.

  • Creating A Jar Archive - Conor’s Blog on 27 Apr 2009 at 9:42 pm

    [...] made the HelloWorld class a few days ago in this post. I am getting a bit ahead of myself here but what if you have an application with many files? Do [...]

Trackback URI | Comments RSS

Leave a Reply



Conor's Blog is powered by Wordpress | Template design by Conor Mac Aoidh