• No results found

The exam objectives state that you need to be able to “ develop code that declares, initializes, and uses primitives, arrays, enums, and objects. ” Declaring these various data types involves creating a variable. A variable represents an allocated piece of memory for storing data. Java is a strongly typed programming language, meaning every variable must be declared with a specifi c data type before it can be used. Declaring a variable involves stating the data type and giving the variable a name. For example, the following statements declare three variables; an int named channel , a

double named diagonal , and a String reference named brand :

int channel; double diagonal; String brand;

A variable is initialized when it is fi rst assigned a value. For example, the following statements initialize our three variables:

channel = 32; diagonal = 53.0; brand = “Acme”;

In Java, a variable must be initialized before you can use it. Variables that represent fi elds in a class are automatically initialized to their corresponding “ zero ” value during object instantiation. Local variables must be specifi cally initialized. The next section, “ Scoping, ” discusses the initializing of variables in detail.

The name of a variable is referred to as its identifi er . (The names of your fi elds, classes, methods, interfaces, and enums are also identifi ers.) The exam objectives include knowing the “ legal identifi ers for variable names. ” Here are the rules for legal identifi ers:

An identifier is a Unicode character sequence of Java letters and Java digits. These include the ASCII characters A – Z and a – z , the digits 0 – 9 , the underscore character ( _ ), and the dollar sign ( $ ).

The first character of an identifier must be a Java letter, underscore, or dollar sign. (In other words, the first character cannot be a digit.)

c02.indd 78

An identifier must not be a Java keyword, true , false , or null .

Table 2.1 contains a list of valid and invalid identifi ers to demonstrate these rules. Let ’ s take a look at the invalid identifi ers:

T A B L E 2 .1 Java Identifiers

Valid Identifiers Invalid Identifiers

x1 x 1 True true _7_ me@company _firstName 1stName car$model x*y $color seven# Java Tokens

When your source code is compiled, the compiler breaks down your code into tokens based on the spaces, line feeds, tabs and other separators in your code. There are fi ve types of tokens in Java:

Separators Keywords Literals Operators Identifi ers

Because identifi ers are the names you come up with for your variables, classes, fi elds, methods, interfaces (and so on), the compiler needs to be able to recognize them easily. This is why Java needs a specifi c set of rules that must be followed for creating legal identifi ers.

x 1 has a space in it, which is not allowed.

true is a reserved word.

me@company contains the @ symbol, which is not a Java letter or digit.

1stName does not start with a Java letter. Identifiers cannot start with a digit.

x*y contains the multiplication operator. Identifiers cannot contain any of the Java operators.

seven# contains the # symbol, which is also not a Java letter or digit.

Declaring Variables 79 c02.indd 79 c02.indd 79 2/11/09 6:15:35 PM2/11/09 6:15:35 PM

80 Chapter 2 Declarations, Initialization, and Scoping

Scoping

As mentioned previously, the exam objectives state that you need to be able to develop code that uses “ static, instance, and local variables. ” Each of these three types of variables has a different scope. Scope refers to that portion of code where a variable can be accessed. There are three kinds of variables in Java, depending on their scope:

Instance variables These variables represent the nonstatic fi elds of a class. Class variables These variables represent the static fi elds of a class.

Local variables These variables are defi ned inside a method. Local variables are only accessible within the method in which they are declared.

This section discusses these three types of variables in detail, starting with a discussion of instance variables.

Instance Variables

Instance variables are the nonstatic fi elds of your class, often referred to simply as fi elds . These variables get allocated in memory when a new object is instantiated. Because the new operator zeroes the memory for an object, all fi elds initially have their corresponding zero value, which are as follows:

Primitive numeric fields initialize to 0. This includes byte , short , int , long , float and

double .

boolean types initialize to false .

char types initialize to the null character ‘ \u0000 ’ . Reference types initialize to null .

Instance variables are always initialized during object instantiation, so you can use an instance variable even if you do not specifi cally assign it a value.

Let ’ s take a look at an example. Suppose we have the following class named Television :

1. public class Television { 2. public int channel; 3. public double diagonal; 4. public String brand; 5. 6. public Television() { 7. channel = 4; 8. } 9. } c02.indd 80 c02.indd 80 2/11/09 6:15:35 PM2/11/09 6:15:35 PM