Java Reference
In-Depth Information
These objectives are Section 1 of the SCJP exam objectives.
The exam tests your knowledge of all aspects of declaring a
Java class, including the details of declaring fi elds, methods,
and constructors. The exam also tests your knowledge of declaring interfaces, enums,
arrays, and nested classes. This chapter covers all of these topics in detail.
Declaring Variables
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.)
Search WWH ::




Custom Search