Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Declaring Variable Types

Java variables fall into one of two categories: primitives and references to objects. This section discusses these two variable types.

Your programs will likely use various primitive variables. Any variable you declare of type byte, short, int, long, float, double, char, or boolean is a primitive variable. The following are examples of variable declarations for each primitive type:

byte x;
short nDaysInMarch;
int nCounter;
long lBacteriaCount;
float fAccountBalance;
double dExactBalance;
char cMiddleInitial;
boolean bQuit;

Reference variables are used to store references, or pointers, to objects. (Keep in mind that I’m not referring to pointers such as those used in C and C++ here, but simply the concept of pointing to an object in memory.) These objects can be class instances, class instances that implement interfaces, or arrays.

The following are examples of reference variable declarations for each type:

String strHelloString;  // Class instance.
AudioClip Music;  // Class instance of AudioClip, an interface.
int nHighScores[];  // Array of integers.

Initializing and Storing Values in Variables

After a variable has been declared, a value can be stored in it. This can be done either when a variable is declared—a process known as initialization (assigning a value to the variable)—or anytime after it has been declared. In either case, any value assigned to the variable must be of the same type as the variable itself.

These are examples of variables being initialized at the time of declaration:

byte x = 3;
short nDaysInMonth = 30;
int nCounter = 900;
long nBacterialCount = 12249123;
float fAccountBalance = 152.76;
double dExactBalance = 452.77;
char cMiddleInitial = 'R';
boolean bQuit = false;
String strHelloString = "Hello World";
AudioClip Music = getAudioClip( getCodeBase(), "hello.au");

In each of the preceding examples, a value consistent with the variable’s data type is assigned to it at the time of declaration. These variables have been initialized. From the moment the variables are created, they contain a value. Notice, however, that there is no example of an array being initialized.

Initializing an Array

In the case of arrays, each element can contain a value. If you declare an array to have a dozen integer elements, it can hold 12 different integers.

When an array is initialized, it’s done in two steps. First you declare the number of elements the array will have. Then each element in the array is individually initialized, or set to a value (usually zero). Here is an example of how you would initialize an array of integers:

int nHighScores[] = new int[12]; // Declare for 12 integers.
for( short i=0; i<12; i++)
    nHighScores[i] = 0; // Initialized each to zero.

As you can see, the for loop used previously also contains a variable declaration. You declare an index, i, that is used to access each element in the array. Before using i, you initialize it to zero, because the first element in every array is at position zero. After 12 iterations, each element in the array has been initialized to zero.


Caution:  You can access a variable only within its scope. Because the first element of every array is at position zero, the scope of a 12-element array runs from 0 to 11. If you attempt to access a variable outside its scope, the compiler generates an error.

Every variable has an associated scope, which is the extent to which it can be used. The scope of a variable begins immediately where it is declared and ends with the closing brace (}) of the block of code in which it is declared.

In the following example, I use three local variables in a method: i, j, and, k. The variable i is in scope for the entire method. The variable j is in scope inside of the for loop, and the variable k is in scope within the if conditional.

public void Foo( void)
{
    int i;
    for( i=0; i<12; i++)
    {
        int j;
        j = i * 8;
        if( j == 16)
        {
            int k;
            k = j + i;
        }// End of scope for k.
        // Trying to access k here will result in a compiler error.
    }// End of scope for j.
    // Trying to access j here will result in a compiler error.
}// End of scope for i.

Variables that are declared as member variables of a class have a valid scope throughout the class. For instance, the Variables applet in Listing 2.1 has three member variables: m_nHorizontal, m_nVertical, and m_bButton. These variables can be accessed in any of the applet’s methods.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.