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

Trail: Learning the Java Language
Lesson: Language Basics

Variables

An object stores its state in variables.

Definition:  A variable (in the glossary) is an item of data named by an identifier.
You must explicitly provide a name and a type for each variable you want to use in your program. The variable's name must be a legal identifier (in the glossary) — an unlimited-length sequence of Unicode characters that begins with a letter. You use the variable name to refer to the data that the variable contains. The variable's type determines which values it can hold and what operations can be performed on it. To give a variable a type and a name, write a variable declaration (in the glossary), which generally looks like this.
type name
In addition to the name and type that you explicitly give a variable, a variable has scope. The section of code where the variable's simple name can be used is the variable's scope (in the glossary). The scope is determined implicitly by the variable declaration's location; that is, where the declaration appears in relation to other code elements. You'll learn more about this in the Scope (in the Learning the Java Language trail) section.

The boldface type in the following program, called MaxVariablesDemo (in a .java source file), highlights all the variable declarations in the program.

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

        //integers
        byte largestByte = Byte.MAX_VALUE;
        short largestShort = Short.MAX_VALUE;
        int largestInteger = Integer.MAX_VALUE;
        long largestLong = Long.MAX_VALUE;

        //real numbers
        float largestFloat = Float.MAX_VALUE;
        double largestDouble = Double.MAX_VALUE;

        //other primitive types
        char aChar = 'S';
        boolean aBoolean = true;

        //Display them all.
        System.out.println("The largest byte value is "
                           + largestByte + ".");
        System.out.println("The largest short value is "
                           + largestShort + ".");
        System.out.println("The largest integer value is "
                           + largestInteger + ".");
        System.out.println("The largest long value is "
                           + largestLong + ".");

        System.out.println("The largest float value is "
                           + largestFloat + ".");
        System.out.println("The largest double value is "
                           + largestDouble + ".");

        if (Character.isUpperCase(aChar)) {
            System.out.println("The character " + aChar
                               + " is uppercase.");
        } else {
            System.out.println("The character " + aChar
                               + " is lowercase.");
        }
        System.out.println("The value of aBoolean is "
                           + aBoolean + ".");
    }
}
This is the output from this program.
The largest byte value is 127.
The largest short value is 32767.
The largest integer value is 2147483647.
The largest long value is 9223372036854775807.
The largest float value is 3.4028235E38.
The largest double value is 1.7976931348623157E308.
The character S is uppercase.
The value of aBoolean is true.
The following sections further explore the various aspects of variables: The MaxVariablesDemo program uses two items with which you might not yet be familiar and are not covered in variable discussion: several constants named MAX_VALUE and an if-else statement. Each MAX_VALUE constant is defined in one of the number classes provided by the Java platform and is the largest value that can be assigned to a variable of that numeric type. These classes are covered in the Numbers (in the Learning the Java Language trail) section. The if-else statement is covered later in this chapter in The if and else Statements (in the Learning the Java Language trail) section.

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

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.