The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

Variables and Data Types

Variables are the nouns of a programming language-that is, they are the entities (values and data) that act or are acted upon. The countChars method defines two variables-- count and in. The program increments count each time it reads a character from the other variable in. The declarations for both variables appear in bold in the following listing:
import java.io.*;
public class Count {
    public static void countChars(Reader in) throws IOException
    {
        int count = 0;

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

Data Types

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

There are two major categories of data types in the Java language: primitive and reference. The following table lists, by keyword, all of the primitive data types supported by Java, their sizes and formats, and a brief description of each.


Type Size/Format Description
(integers)
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)


Purity Tip:  In other languages, the format and size of primitive data types may depend on the platform on which a program is running. In contrast, the Java language specifies the size and format of its primitive data types. Hence, you don't have to worry about system-dependencies.

A variable of primitive type contains a single value of the appropriate size and format for its type: a number, character, or boolean value. For example, the value of an int is an integer, the value of a char is a 16-bit Unicode character, and so on. The value of the count variable in countChars ranges from its initial value of zero to a number that represents the number of characters in the input.

Arrays, classes, and interfaces are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to the actual value or set of values represented by the variable. A reference is like your friend's address: The address is not your friend, but it's a way to reach your friend. A reference type variable is not the array or object itself but rather a way to reach it.

The countChars method uses one variable of reference type, in, which is a Reader object. When used in a statement or expression, the name in evaluates to a reference to the object. So you can use the object's name to access its member variables or call its methods (just as countChars does to call read).


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 countChars method wants to refer to the value of the count variable, it simply uses the name count.

In Java, the following must hold true for a variable name

  1. It 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. It 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, Cyrillic, and Hebrew. This is important so that programmers can write code that is meaningful in their native languages.
  2. It must not be a keyword or a boolean literal (true or false).
  3. It must not have the same name as another variable whose declaration appears in the same scope.

By Convention:  Variable names begin with a lowercase letter and class names begin with an uppercase letter. If a variable name consists of more than one word, such as isVisible, the words are joined together and each word after the first begins with an uppercase letter.

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. Scope is covered in the next section.

Scope

A variable's scope is the block of code within which the variable is accessible and determines when the variable is created and destroyed. The location of the variable declaration within your program establishes its scope and places it into one of these four categories:

A member variable is a member of a class or an object. It can be declared anywhere in a class but not in a method. The member is available to all code in the class. The Count class declares no member variables. For information about declaring member variables, refer to Declaring Member Variables(in the Learning the Java Language trail) in the next lesson.

You can declare local variables anywhere in a method or within a block of code in a method. In countChars, 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 countChars method (indicated by the first right curly bracket } that appears in the program 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. The discussion about writing methods in the next lesson, Implementing Methods(in the Learning the Java Language trail), talks about passing values into methods through parameters. You can also pass values into constructors in the same manner, so the discussion regarding method parameters applies equally well to parameters to constructors. In countChars, in is a parameter to the countChars method. The scope of a method parameter is the entire method for which it is a parameter. So, in countChars, the scope of in is the entire countChars 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 countChars method does not have any exception handlers, so it doesn't have any exception-handler parameters. Handling Errors with Exceptions(in the Learning the Java Language trail) talks about using Java exceptions to handle errors and shows you how to write an exception handler that has a parameter.

Variable Initialization

Local variables and member variables can be initialized with an assignment statement when they're declared. The data type of both sides of the assignment statement must match. The countChars method provides an initial value of zero 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.

Final Variables

You can declare a variable in any scope to be final, including parameters to methods and constructors. The value of a final variable cannot change after it has been initialized.

To declare a final variable, use the final keyword in the variable declaration before the type:

final int aFinalVar = 0;
The previous statement declares a final variable and initializes it, all at once. Subsequent attempts to assign a value to aFinalVar result in a compiler error. You may, if necessary, defer initialization of a final local variable. Simply declare the local variable and initialize it later, like this:
final int blankfinal;
. . .
blankfinal = 0;
A final local variable that has been declared but not yet initialized is called a blank final. Again, once a final local variable has been initialized it cannot be set and any later attempts to assign a value to blankfinal result in a compile-time error.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search