Previous | Next | Trail Map | Writing Java Programs | The Nuts and Bolts of the Java Language


Variables and Data Types

Variables are the nouns of a programming language: they are the entities (values, data) that act or are acted upon. The character-counting program uses two variables--count and args. The program increments count each time it reads a character from the input source and ignores args. The declarations for both variables appear in bold in the following listing:
class Count {
    public static void main(String[] args)
        throws java.io.IOException
    {
        int count = 0;

        while (System.in.read() != -1)
            count++;
        System.out.println("Input has " + count + " chars.");
    }
}    
A variable declaration always contains two components: the type of the variable and its name. Also, the location of the variable declaration, that is, where the declaration appears in relation to other code elements, determines the scope of the variable.

Variable Types

All variables in the Java language must have a data type. A variable's type determines the values that the variable can have and the operations that can be performed on it. For example, the declaration int count declares that count is an integer (int). Integers can have only whole number values (both positive and negative) and you can use the standard arithmetic operators (+, -, and so on) on integers to perform the standard arithmetic operations (addition, subtraction, multiplication, and division).

There are two major categories of data types in the Java language: primitive types and reference types. Primitive types contain a single value and include types such as integer, floating point, character, and boolean. The following table lists, by keyword, all of the primitive data types supported by Java, their size and format, and a brief description of each.


Type Size/Format Description
(whole numbers)
byte 8-bit two's complement Byte-length integer
short16-bit two's complementShort integer
int32-bit two's complementInteger
long64-bit two's complementLong integer
(real numbers)
float32-bit IEEE 754Single-precision floating point
double64-bit IEEE 754Double-precision floating point
(other types)
char16-bit Unicode characterA single character
booleantrue or falseA boolean value (true or false)

Reference types are called such because the value of a reference variable is a reference (a pointer in other terminology) to the actual value or set of values represented by the variable. For example, the character-counting program declares (but never uses) one variable of reference type, args, which is declared to be an array of String objects. When used in a statement or expression, the name args evaluates to the address of the memory location where the array lives. This is in contrast to the name of a primitive variable, the count variable, which evaluates to the variable's actual value.

Besides arrays, classes and interfaces are also reference types. Thus when you create a class or interface you are in essence defining a new data type. See Objects, Classes, and Interfaces(in the Writing Java Programs trail) for information about defining your own classes and interfaces.


Note to C and C++ Programmers: There are three C Data Types Not Supported By the Java Language. They are pointer, struct, and union. These data types are not necessary in Java; you use classes and objects instead.

Variable Names

A program refers to a variable's value by its name. For example, when the character-counting program wants to refer to the value of the count variable, it simply uses the name count. By convention, variable names begin with a lowercase letter and class names begin with an uppercase letter.

In Java, a variable name:

  1. must be a legal Java identifier comprised of a series of Unicode characters. Unicode is a character coding system designed to support text written in diverse human languages. Unicode allows for the codification of up to 65,536 characters (currently 34,168 have been assigned). This allows you to use characters in your Java programs from various alphabets such as Japanese, Greek, Russian, Hebrew, and so on. This is important so that programmers can write code that is meaningful in their native languages.
  2. must not be the same as a keyword or a boolean literal (true or false).
  3. must not have the same name as another variable whose declaration appears in the same scope.
Rule #3 implies that variables may have the same name as another variable whose declaration appears in a different scope. This is true. In addition, in some situations, a variable may share names with another variable which is declared in a nested scope.

By convention, variables names begin with a lower case letter. If a variable name is comprised of more than one word, such as isVisible, the words are joined together and each word after the first begins with an upper case letter.

Scope

A variable's scope is the block of code within which the variable is accessible. Also, a variable's scope determines when the variable is created and destroyed. You establish the scope of a variable when you declare it. Scope places a variable into one of these four categories: A member variable is a member of a class or an object and is declared within a class (but not within any of the class's methods). The character-counting program declares no member variables. For information about declaring member variables and their scope, refer to Declaring Member Variables(in the Writing Java Programs trail) in Objects, Classes, and Interfaces(in the Writing Java Programs trail).

Local variables are declared within a method or within a block of code in a method. In the character-counting example, count is a local variable. The scope of count, that is, the code that can access count, extends from the declaration of count to the end of the main method (indicated by the first right curly bracket ('}') that appears in the sample code). In general, a local variable is accessible from its declaration to the end of the code block in which it was declared.

Method parameters are formal arguments to methods and constructors and are used to pass values into methods and constructors. The discussion about writing methods in the Implementing Methods(in the Writing Java Programs trail) page talks about passing values into methods and constructors through method parameters. In the character-counting example, args is a method parameter to the main method. The scope of a method parameter is the entire method or constructor for which it is a parameter. So, in the example, the scope of args is the entire main method.

Exception handler parameters are similar to method parameters but are arguments to an exception handler rather than to a method or a constructor. The character-counting example does not have any exception handlers, so it doesn't have any exception handler parameters. Handling Errors using Exceptions(in the Writing Java Programs trail) talks about using Java exceptions to handle errors and shows you how to write an exception handler with its parameter.

Variable Initialization

Local variables and member variables can be initialized when they are declared. The character-counting program provides an initial value for count when declaring it:
int count = 0;
The value assigned to the variable must match the variable's type.

Method parameters and exception handler parameters cannot be initialized in this way. The value for a parameter is set by the caller.


Previous | Next | Trail Map | Writing Java Programs | The Nuts and Bolts of the Java Language