Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Lecture notes · chapter 2

Variables and identifiers

A variable is a named, typed box that points to a memory cell.

It corresponds to several things:

  • its address, which indicates the first location where the variable is stored;
  • its name, which saves you from handling the address directly;
  • its type, which indicates how the information is stored in memory;
  • its value.

Naming rules:

  • Java supports Unicode, but stick to ASCII only, for compatibility reasons.
  • An identifier cannot start with a digit, and cannot contain a space or an operator.
  • Choose a suitable name, written in camelCase.
  • You are not allowed to use a reserved word of the language (such as if, else, break…) as an identifier.

What the compiler accepts and what it rejects

public class Demo {
    public static void main(String[] args) {
        int nombreDeCanards = 3;   // camelCase: correct
        int NombreDeCanards = 4;   // valid, but NOT the same variable
        System.out.println(nombreDeCanards + " then " + NombreDeCanards);

        // Uncomment these lines to see the compiler errors:
        // int 1ereNote = 12;
        // int mon age = 18;
        // int class = 1;
    }
}