Hello World – Java
Posted Apr 26th, 2009 by ConorI 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!
// c version
#include “stdio.h”
void main() {
printf(“hello world!”);
}
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.
[...] 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 [...]