The Nuts and Bolts of the Java Language |
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
andargs
. The program incrementscount
each time it reads a character from the input source and ignoresargs
. The declarations for both variables appear in bold in the following listing: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.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."); } }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 declarationint count
declares thatcount
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, and so on). 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 short 16-bit two's complement Short integer int 32-bit two's complement Integer long 64-bit two's complement Long integer (real numbers) float 32-bit IEEE 754 Single-precision floating point double 64-bit IEEE 754 Double-precision floating point (other types) char 16-bit Unicode character A single character boolean true or false A 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 nameargs
evaluates to the address of the memory location where the array lives. This is in contrast to the name of a primitive variable, thecount
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 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 thecount
variable, it simply uses the namecount
. By convention, variable names begin with a lower case letter (class names begin with a capital letter).In Java, a variable name:
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.
- 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.
- must not be the same as a keyword or a boolean literal (
true
orfalse
)- must not have the same name as another variable whose declaration appears in the same 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 next lesson, Objects, Classes, and Interfaces.
- member variable
- local variable
- method parameter
- exception handler parameter
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 ofcount
, that is, the code that can accesscount
, extends from the declaration ofcount
to the end of themain()
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 on the Implementing Methods page in the next lesson talks about passing values into methods and constructors through method parameters. In the character-counting example,
args
is a method parameter to themain()
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 ofargs
is the entiremain()
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 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 forcount
when declaring it:The value assigned to the variable must match the variable's type.int count = 0;Method parameters and exception handler parameters cannot be initialized in this way. The value for a parameter is set by the caller.
The Nuts and Bolts of the Java Language |