Course Content‎ > ‎Session 2‎ > ‎

Intro to Variables

Programs are written to process different kinds of data in order to produce useful information.  This data can consist of numbers, characters, strings, dates. The task of processing the data is accomplished by executing a sequence of precise instructions. These instructions are formed using certain words and symbols, according to rules known as syntax rules. Every programming language has its own syntax rules, which have to be followed strictly  in order to execute the program successfully. In this session, we will learn what kind of data is supported by Java and how we can store it, before Java program can process it to produce some useful information. 


Variables can be used to store data. Variables in any programming language are meant to hold the data, that is expected to change, any time during the course of the program. Whenever you declare a variable, Java sets up an area in the memory of the computer to store the data. The data stored in the variable can be changed any time. 

For example:

    int age = 30;

In the above statement, the variable is age and the data it holds is 30. We can change the contents of the variable age at any place in the program.

Variables are directly related to the memory. Memory is a place where data and instructions are stored and this data can be retrieved, whenever required. A computer memory is provided with locations, where the data can be stored.  Each memory location is identified by a unique address. These locations store the data in the form of bits. Group of 8 bits make up one byte. Computer memory is expressed in terms of bytes. The address of a memory location is actually address of a byte.  The memory address location starts from 0, thus the address of the first byte is 0. A 256 byte memory has 256 address locations starting from 0 to 255.

Rules for Variable Names

Every variable has a name, like in the above statement, the variable names is age. The rules that have to be followed for a variable name are:

  • Variable name must begin with a letter.
  • Variable name can be a sequence of  letters ( 'A'-'Z',  'a'-'z',  '_' or any Unicode character that denotes a letter in a   language) and digits (0-9).
  • Symbols like @, +, -, * or spaces etc. cannot be used in a variable name.
  • All characters in a variable name are significant and variable names are case-sensitive. This means variable name Total is not the same as the variable name total.
  • Variables in Java can have any length.
  • You cannot use a Java reserved word for a variable name. To see a list of keywords reserved for Java, please see Appendix1.

Before we can use the variables in our program, we have to declare them. Since the variables are the name of the storage locations where the data will be stored, so it is very important that we declare these variable names to the compiler.