Previous lesson Back to the front Next lesson

Hello, World!


Congratulations, you are about to write the world's simplest Java program. Millions of people have learned to program in all sorts of different languages, and most of them started with a little program called "Hello, World!".

So to begin, bring up a text editor like Notepad++ and copy and paste in the following code.


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

In case you can't read it, the bit after "String" is two square brackets, an opening one and then immediately a closing one (they're next to the P key). You should lay it out just like I have - use the tab key. Press tab once on the line just before public static... and twice just before System.out.println..., then once on the second to last line (the one with only a } on it).

It's probably worth mentioning at this point that capitals do matter in Java, a lot. You can have something called abc and something else called ABC and they are as different as if they were called chalk and cheese. Computers are more picky than the bogeyman.

In case this isn't obvious, from now on I'm going to put stuff that I want you to type in (ie programs) on a green background, and stuff that you see on the screen or whatever on a red background. OK?

Once you think your version looks like mine, save it. You must use the name Hello.java when you do.

Let's quickly explain the program, and then go on to the next lesson.

class Hello { Think of Hello as the title of the program. We'll come back to exactly what class means later. For now, just remember to save the file as Hello.java if it says class Hello { at the top.
	public static void main(String[] args) {
For now all you need to know about this line is that, when you run a Java program, it has to have a line exactly like this one. This just tells it where to start. Notice the word main? This is the main part of the program.
		System.out.println("Hello, world!");
This line is a statement which tells the computer to do something. In this case it tells it to print out a message on the screen. The message is the bit between the two " marks. In fact in this program it is the only line which actually does something. As with any statement, it must end with a semicolon(;).
	}
There are these things called blocks which start with a { and end with a }. This program has two blocks, one inside the other. This line ends the innermost block, which started on the second line with public static void main(String[] args) {
} Can you work out what this does? It ends the outermost block, which started on the first line with class Hello {

Great, so you've typed in your first Java program and you've attempted to understand it (don't worry if you don't!). I bet you want to actually make it do something now.

Make it go! Make it go!

Too patronising? Too complex? Typing error? Offended by traffic cones? Got a question or something I should add? Send an email to ben_golding@yahoo.co.uk !

visits to this site

The contents of this site are copyright of Ben Golding