,

Basic Language Elements – Basic Elements, Primitive Data Types, and Operators

2.1 Basic Language Elements

Like any other programming language, the Java programming language is defined by grammar rules that specify how syntactically legal constructs can be formed using the language elements, and by a semantic definition that specifies the meaning of syntactically legal constructs.

Lexical Tokens

The low-level language elements are called lexical tokens (or just tokens) and are the building blocks for more complex constructs. Identifiers, numbers, operators, and special characters are all examples of tokens that can be used to build high-level constructs like expressions, statements, methods, and classes.

Identifiers

A name in a program is called an identifier. Identifiers can be used to denote classes, methods, variables, and labels.

In Java, an identifier is composed of a sequence of characters, where each character can be either a letter or a digit. However, the first character in an identifier must always be a letter, as explained later.

Since Java programs are written in the Unicode character set (p. 37), characters allowed in identifier names are interpreted according to this character set. Use of the Unicode character set opens up the possibility of writing identifier names in many writing scripts used around the world. As one would expect, the characters A to Z and a to z are letters and the characters 0 to 9 are digits. A connecting punctuation character (such as underscore _) and any currency symbol (such as $, ¢, ¥, or £) are also allowed as letters in identifier names, but these characters should be used judiciously. Note also that the underscore (_) on its own is not a legal identifier name, but a keyword (Table 2.1, p. 31).

Table 2.1 Keywords in Java

abstractdefaultifprivatethis
assertdoimplementsprotectedthrow
booleandoubleimportpublicthrows
breakelseinstanceofreturntransient
byteenumintshorttry
caseextendsinterfacestaticvoid
catchfinallongstrictfpvolatile
charfinallynativesuperwhile
classfloatnewswitch_ (underscore)
continueforpackagesynchronized 

Identifiers in Java are case sensitive. For example, price and Price are two different identifiers.

Examples of Legal Identifiers

Click here to view code image number, Number, sum_$, bingo, $$_100, _007, mål, grüß

Related Posts