Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 103

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

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх