Witscale Test Center

7.1 Keywords and identifiers


7.1 Keywords and identifiers

All Java keywords are lowercase predefined words, which have special meaning in Java. They are special words which you cannot use to name the classes, methods, or variables. Table 7.1 shows all the keywords and reserved words.


 

Table 7.1 Java keywords and reserved words

 

abstract

default

if

private

throw

assert1

do

implements

protected

throws

boolean

double

import

public

transient

break

else

instanceof

return

true2

byte

extends

int

short

try

case

false2

interface

static

void

catch

final

long

strictfp

volatile

char

finally

native

super

while

class

float

new

switch

 

const3

for

null2

synchronized

 

continue

goto3

package

this

 

 

 

1

The assert is a keyword as per the version 1.4 of Java. The older versions of Java do not consider it as a keyword or reserved word.

 

2

The true and false are reserved word. Technically, they are notb keywords but are boolean literals. Similarly, the null is a reserved word. Technically, it is not the keyword but a null literal.

 

3

The const and goto are unused reserved words in Java.

 

 

You should be able to identify all Java programming language keywords in the table. There are total 52 reserved words and keywords. Out of these 52, const, goto and the three literals (true, false and null) are not really keywords; the rest are keywords. You will learn the details of each one of them during the course of this book. For now, remember each keyword is understood by the compiler differently.

 

The exam expects you to know all the valid Java keywords. You may be asked to choose valid keywords. Be careful about the keywords from other languages. For instance, keywords such as include, define, signed, decimal etc are not Java keywords or reserved words.

 

Identifiers are the words used by the programmers to name a variable, a class, a method, an interface or a label. Note that identifiers can never be the same as any of the keywords or reserved words listed in table 7.1. An identifier must begin with either a letter (any alphabet a-to-z) , an underscore(_) or the dollar sign($). It can subsequently contain either letters, dollar signs, underscores or digits. Table 7.2 shows some valid words that can be used as identifiers along with some invalid words.

 

Table 7.2 Few examples of valid and invalid identifiers

 

Example Identifiers

Validity

Reason

MyClass

Valid

 

integer

Valid

integer is not a keyword.

$$$$

Valid

An identifier can start with a $.

8wonders

Invalid

An identifier cannot start with a digit.

_1234

Valid

An identifier can start with a  _

publicinterface

Valid

Embedded keywords are allowed. public and interface are keywords, but publicinterface is a valid identifier.

*wars

Invalid

Must start with $, _ or a letter.

The%Value

Invalid

Cannot have any characters other than $, _ or digits and letters.

Int

Valid

int is a keyword , Int is not a keyword.

 

Since a valid identifier is never a keyword, you need to know all the Java keywords to make sure that the identifier is indeed valid. The Java keywords are case sensitive. class is a Java keyword but Class is not. You can therefore use the word Class as a valid Java identifier.

 

You need not worry whether the reserved words (const and goto) and literals (true false and null) are considered as keywords or identifiers. Technically speaking,  reserved words are identifiers that can be used only as keyword. Fortunately a note in the SCJP objective 4.4 clearly says “There will not be any questions regarding esoteric distinctions between keywords and manifest constants”. It means that you will not be asked to distinguish between these valid reserved words and keywords. The manifest constant mentioned here is actually referring to the literal constants (true, false and null). In general, the manifest constant is any constant value that is refereed by name. The literals are manifest constants by definition. We will learn about literals in section 7.3