|
|||||||||||||||||||||||||||
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.*;
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
There are two major categories of data types in the Java language:
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. 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
Variable Initialization Local variables and member variables can be initialized with an
assignment
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
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
final int blankfinal;
A final variable that has been declared but not yet initialized is called
a
|
|||||||||||||||||||||||||||
For CS 763(Web Seminar) at SUNY Buffalo |