Our first program will simply output "Hello World". 

 

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.)

The Java compiler and interpreter are case-sensitive, so you must capitalize consistently. 

 

 
While it is stored as text, it cannot be run as a program on the computer.  It must first be translated into machine language (bytecode) using a compiler. The java compiler is a program called javac.exe.  Once it is compiled, the file of bytecode will be called Hello.class  The bytecode file will contain exactly the same bytecodes no matter what computer system is used.

Java bytecode is machine instructions for a Java processor chip.  Java bytecode can only be run directly on a Java processor chip.  Most computer systems do not have a Java processor chip, so they need an interpreter program to enable the non-Java processor to become a Java virtual machine. That is, it looks like a computer with a Java processor chip and can run Java bytecode. Each type of computer system has its own Java interpreter.  The Java interpreter for Windows is named Java.exe

An applet is a Java bytecode program that runs on a Web browser. Most up-to-date Web browsers include a Java interpreter.

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

 

 
Java is case sensitive.  In Java code Main and main are different.

Usually Java ignores extra white space used for formatting text.

Braces { } are grouping symbols which mark the beginning and end of a program section.  You must have matching pairs of braces.

Every Java statement ends with the ; symbol.  A statement is a program expression that generates a processing action.  Not every Java expression is a statement.

Comments are text that explains your program.  The compiler will ignore comments.  // creates a comment that extends to the end of the line. 
/* and */ symbols mark the beginning and end of a comment that goes over more than one line. 

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. 

 

Summary

To create a Java program, you must ensure that the name of the class in the file is the same as the name of the file (and that the file has the extension .java).

To compile the program use the command javac

javac HelloWorld.java

This creates the Java ByteCode in a file HelloWorld.class.

To run the compiled program use the command java

java HelloWorld

Note that you do not use the any file extension in this command.

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
//Created by M Meijers July 2006
public class Loops
   {
    public static void main ( String[] args )
      {
       for (int count=1; count <=10; count++)
           {
           System.out.print("This is loop number ");
           System.out.println(count);
            }
         }
     }

 
count is a variable.   Variables need to be declared before you can use them.  To declare a variable you specify its name and type.  (In this case we are declaring count as an integer type).  This ensures that the compiler sets aside memory space to store the variable.
 What is the difference between print and println? 

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
//Created by M Meijers July 2006
//Demonstrates importing Java library

import java.util.*;

  public class TodaysDate
     {
      public static void main (String[] args)
              {
               System.out.print ("Hello, Today it's ");
               System.out.println(new Date());
               }
      }