Our first program will simply output "Hello World".
- First we will create a text file with the required code (the source file)
- Then we will compile it into bytecode (The compiler program javac will translate our text file into language the Java Interpreter can understand.)
- Finally we will run the interpreted file using the Java Virtual Machine.
Here is an example of a simple Java program
//Simple Java program to print Hello World! //Created by M Meijers March 2005 public class Hello
{
public static void main ( String[] args )
{
System.out.println("Hello World!");
}
}
|
Create the program using a text editor such as Notepad and save and name it Hello.java This is what is known as a source file. (Make sure you save as type 'All files' so that you do not get a .txt file extension added on.)
![]() |
|
![]() |
|
Now open a command window. In XP click Start > Run and type Cmd
Now change directory to where you saved your Hello.java file
eg If you saved it on your H drive in a folder called java, you would need to type
H: cd java |
Now type javac Hello.java (Watch capitalisation!)
If it compiled correctly you should get no message.
Now to run your program type java Hello
You should get the output: Hello World!
If you receive error messages try looking here for help http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#1
![]() |
|
Now we will look at the code line by line:
|
//Simple Java program to print Hello World!
//Created by M Meijers March 2005 |
The first two lines are the program Header. These are comment lines that tell the reader about the program.
|
public class Hello
{ |
public and class are keywords. public means that other parts of the program can use this code. class is necessary because everything in a Java program must be in a class. The first class in a program (called the driving class) must have the same name as the file in which it is stored. The source file must have the extension .java
|
public static void main ( String[] args ) |
This line creates the first method of the program. A method is a piece of program that performs a specific action. Every Java program must have a method named main() The main() method is public static and void. Void means that it returns nothing. Each class can have a main() method, but execution always starts with the class that has the same name as the source file. We will look at methods more later.
|
System.out.println("Hello World!");
|
This line displays the message 'Hello World' on the screen.
System is a built in class that is part of the Java libraries.
out is an object of this class.
println is a method of System.out that prints a single line of text (usually) on the screen.
"Hello World" is the string to be printed.
As we move further into Java you will come to understand more of what all this means.
![]() |
|
Let's modify the program a bit. Try the following:
//Simple Java program to print Hello World! //Created by M Meijers July 2006 public class Hello
{
public static void main ( String[] args )
{
System.out.println("Hello World!");
System.out.println("I can make Java programs");
System.out.println("How smart am I?");
}
}
|
Next we will add a loop. See if you can work out how it is working. (Hint: count++ increments the value of count) Copy it into Notepad and save it as Loops.java
|
//Simple Java program to demonstrate a loop |
![]() |
|
![]() |
|
The following program shows how to print out today's date. The utility library (util) is needed to be able to use the date function.
Make sure that you save this program as TodaysDate.java
|
// Program to print out today's date |





