ТОП просматриваемых книг сайта:
OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн.Название OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Год выпуска 0
isbn 9781119864592
Автор произведения Jeanne Boyarsky
Жанр Программы
Издательство John Wiley & Sons Limited
All of the \"
escape the "
. There is one space of essential whitespace on the doe
and deer
lines. All the other leading whitespace is incidental whitespace.
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 multiple 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
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
|
* The reserved words const
and goto
aren't actually used in Java. They are reserved so that people coming 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; // 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