Java Tutorial 1: Beginning Java

Downloads and Getting Started

To start developing Java software, the first thing to do is to download the latest JDK (Java Development Kit). You can download the JDK here from It's free, and it includes everything you need to develop Java programs except an IDE (integrated development environment).

In fact you can choose to download the version with Netbeans bundled in; Netbeans is a development environment, but probably not the most popular one currently used by software professionals (Eclipse and IntelliJ IDEA are probably more popular).

There are various different JDKs, but probably you'll want the SE (Standard Edition), with or without Netbeans.

After downloading the JDK, you may need to add the bin directory of your installation to your PATH variable, so that you can type for instance 'java' on the command line and your operating system will know where to look for the program. The PATH variable just contains a list of paths to folders containing executables. See here for instructions on how to modify it.

Creating Your First Java Program

It's a good idea to create your first programs in Java without using a fancy IDE. That way you can get a feel for how the command-line tools work, and you will know how to fix compilation problems later on.

Download and install a simple text editor for programmers; Notepad++ is a good choice. Now create a directory to put your program in. Save the following text in a file called

public class Animal {
    public static void main(String [] args) {

Now open a command line session (in Windows, go to the Start Menu -> run -> type 'cmd' and click OK).

cd to the directory where you saved

Type the following command:


You will now have a file called Animal.class if all went well. To execute Animal.class, type java Animal:

java Animal

You can see the program is executed and the output displayed on the command line. All this little program does is to write "grrrrrr".

Some Notes About the Basic Program

If you're new to Java, this basic program might look like Greek to you. Unless you're Greek, in which case it might look Chinese. It is best to type the program out yourself, without worrying too much about the details of the program, all of which we will examine further later on.

For now it is enough to note the following

a) The program contains a single public class called Animal
b) The Animal class contains a main 'method', which is the entry (starting) point of the program
c) The main method simple writes the text 'grrrrr' to the console using the highly-useful System.out.println() method.

Declaring and Initializing Variables

Often in programming you want to store a piece of information somewhere and to refer to it later by a name. A language feature called variables allow you to do this.

In Java, there are eight 'primitive' variable types. They are as follows (but for the moment don't worry too much about trying to memories them all; you will remember them if you practice using them, in time):

boolean, byte, char, short, int, long, float, double

The following program illustrates how to declare a variable of each type and assign a value to it.

Note that this program also contains comments. Comments can consist of whatever text you like, but they must start with //, or else be surrounded entirely by / and /.

public class Variables {
    public static void main(String [] args) {

        // This is a comment
        /* This is also a comment */

        // Enough comments, let's get started!
        byte aByte = 5;
        short aShort = 55;
        int anInt = 777;
        long aLong = 12345;
        float aFloat = 1.23f;
        double aDouble = 7.542;
        boolean aBool = true;
        char aChar = 'S';
        // We can print any of these using System.out.println()

Save this text as (the class name must match the file name!) and execute it by first compiling it and then running it using the java command:


>java Variables

  • byte is an 8-bit type (i.e. it can hold only 8 bits of data) and is often used in arrays, for when you want to work with a certain specific number of bytes.

  • char is a 16-bit type, often used for holding unicode characters.

  • int is a 32-bit integer value, i.e. 7, 812, 59 or whatever.

  • short is also an integer value, but can hold less bytes of data than the standard integer. Actually it's a 16-bit type.

  • long similarly is a 'longer' version of the standard int, able to hold 64 bits of data. So basically, use int most of the time, but short if you only need to work with small numbers (less than about +/- 32,767) and long if you need to hold really huge integers.

  • double is used to hold floating-point numbers; and is usually the default choice for a number with a decimal point in it in the same way that int is the default choice for integers.

  • float is a shorter version of double; whereas double is 64-bit, float is only a 32-bit value.

  • Finally, boolean can hold the value true or the value false.

Expressions and Statements

At this point it's worth mentioning some terminology. An expression means any syntactically-correct unit of the language that evaluates to a single value, for instance

"Hello world"
x = 10
"Hi " + "there"

and so on. All of these expressions boil down to a single value (namely, "Hello world", 10 and "Hi there", respectively).

A statement on the other hand, is made up of one or more expressions and ends in a semi-colon, for instance

System.out.println("Hello world");
int x = 10;
System.out.println("Hi " + "there");