Declaring Variables
You’ve seen some variables already. A variable is a name for a piece of memory that stores data. When you declare a variable, you need to state the variable type along with giving it a name. Giving a variable a value is called initializing a variable. To initialize a variable, you just type the variable name followed by an equal sign, followed by the desired value. This example shows declaring and initializing a variable in one line:
String zooName = “The Best Zoo”;
In the following sections, we look at how to properly define variables in one or mul-tiple lines.
Identifying Identifiers
It probably comes as no surprise to you that Java has precise rules about identifier names. An identifier is the name of a variable, method, class, interface, or package. Luckily, the rules for identifiers for variables apply to all of the other types that you are free to name.
There are only four rules to remember for legal identifiers:
Don’t worry—you won’t need to memorize the full list of reserved words. The exam will only ask you about ones that are commonly used, such as class and for. Table 1.9 lists all of the reserved words in Java.
TABLE 1. 9 Reserved words
abstract | assert | boolean | break | byte |
case | catch | char | class | const* |
continue | default | do | double | else |
enum | extends | final | finally | float |
for | goto* | if | implements | import |
instanceof | int | interface | long | native |
new | package | private | protected | public |
return | short | static | strictfp | super |
switch | synchronized | this | throw | throws |
transient | try | void | volatile | while |
There are other names that you can’t use. For example, true, false, and null are literal values, so they can’t be variable names. Additionally, there are contextual keywords like module in Chapter 12. Prepare to be tested on these rules. The following examples are legal:
long okidentifier;
float $OK2Identifier;
boolean _alsoOK1d3ntifi3r;
char __SStillOkbutKnotsonice$;
These examples are not legal:
int 3DPointClass; | // identifiers | cannot begin with a number | |||||||
byte hollywood@vine; | // @ | is | not | a letter, | digit, $ | or _ | |||
String *$coffee; | // * | is | not | a letter, | digit, $ | or _ | |||
double public; | // | public is a | reserved word | ||||||
short _; | // | a | single | underscore is not allowed |
camelCase and snake_case
Although you can do crazy things with identifier names, please don’t. Java has conventions so that code is readable and consistent. For example, camel case has the first letter of each
word capitalized. Method and variable names are typically written in camel case with the first letter lowercase, such as toUpper(). Class and interface names are also written in camel case, with the first letter uppercase, such as ArrayList.
Another style is called snake case. It simply uses an underscore (_) to separate words. Java generally uses uppercase snake case for constants and enum values, such as
NUMBER_FLAGS.
The exam will not always follow these conventions to make questions about identifiers trickier. By contrast, questions on other topics generally do follow standard conventions. We recommend you follow these conventions on the job.