Declaring Variables – Building Blocks – 1Z0-829 Study Guide

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:

  • Identifiers must begin with a letter, a currency symbol, or a _ symbol. Currency symbols include dollar ($), yuan (¥), euro (€), and so on.
  • Identifiers can include numbers but not start with them.
  • A single underscore _ is not allowed as an identifier.
  • You cannot use the same name as a Java reserved word. A reserved word is a special word that Java has held aside so that you are not allowed to use it. Remember that Java is case sensitive, so you can use versions of the keywords that only differ in case. Please don’t, though.

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

abstractassertbooleanbreakbyte
casecatchcharclassconst*
continuedefaultdodoubleelse
enumextendsfinalfinallyfloat
forgoto*ifimplementsimport
instanceofintinterfacelongnative
newpackageprivateprotectedpublic
returnshortstaticstrictfpsuper
switchsynchronizedthisthrowthrows
transienttryvoidvolatilewhile
  • The reserved words const and goto aren’t actually used in Java. They are reserved so that people com-ing from other programming languages don’t use them by accident—­and, in theory, in case Java wants to use them one day.

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;// identifierscannot begin with a number
byte hollywood@vine;// @isnota letter,digit, $or _
String *$coffee;// *isnota letter,digit, $or _
double public;//public is areserved word
short _;//asingleunderscore 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.