The following topics were discussed in this chapter:
Chapter 2. Language Fundamentals
2.1 Basic Language Elements
Like any other programming language, the Java programming language is defined by
that specify how
legal constructs can be
The low-level language elements are called
for short) and are the building blocks for more complex constructs. Identifiers,
In Java an
Identifiers in Java are case sensitive, for example, price and Price are two different identifiers.
Examples of Legal Identifiers:
number, Number, sum_$, bingo, $$_100, ml, gr
Examples of Illegal Identifiers:
48chevy, all@hands, grand-sum
The name 48chevy is not a legal identifier as it starts with a digit. The character @ is not a legal character in an identifier. It is also not a legal operator so that all@hands cannot not be interpreted as a legal expression with two operands. The character - is also not a legal character in an identifier. However, it is a legal operator so grand-sum could be interpreted as a legal expression with two operands.
Keywords are reserved identifiers that are predefined in the language and cannot be used to denote other entities. All the keywords are in lowercase, and incorrect usage results in compilation errors.
Keywords currently defined in the language are listed in Table 2.1. In addition, three identifiers are reserved as predefined literals in the language: the null reference and the Boolean literals true and false (see Table 2.2). Keywords currently reserved, but not in use, are listed in Table 2.3. All these reserved words cannot be used as identifiers. The index contains references to relevant sections where currently defined keywords are explained.
Table 2.1. Keywords in Java
Table 2.2. Reserved Literals in Java
Table 2.3. Reserved Keywords not Currently in Use
A literal denotes a constant value, that is, the value a literal represents remains unchanged in the program. Literals represent numerical (integer or floating-point), character, boolean or string values. In addition, there is the literal null that represents the null reference.
Table 2.4. Examples of Literals
Integer data types are comprised of the following primitive data types: int , long , byte , and short (see Section 2.2).
The default data type of an integer literal is always int , but it can be specified as long by appending the suffix L (or l ) to the integer value. Without the suffix, the long literals 2000L and 0l will be interpreted as int literals. There is no direct way to specify a short or a byte literal.
In addition to the decimal number system, integer literals can also be specified in octal ( base 8 ) and hexadecimal ( base 16 ) number systems. Octal and hexadecimal numbers are specified with and 0x (or 0X ) prefix respectively. Examples of decimal, octal and hexadecimal literals are shown in Table 2.5. Note that the leading (zero) digit is not the uppercase letter O . The hexadecimal digits from a to f can also be specified with the corresponding uppercase forms ( A to F ). Negative integers (e.g. -90 ) can be specified by prefixing the minus sign ( - ) to the magnitude of the integer regardless of number system (e.g., -0132 or -0X5A ). Number systems and number representation are discussed in Appendix G. Java does not support literals in binary notation.
Table 2.5. Examples of Decimal, Octal, and Hexadecimal Literals
Floating-point data types come in two flavors: float or double .
The default data type of a floating-point literal is double , but it can be explicitly designated by appending the suffix D (or d ) to the value. A floating-point literal can also be specified to be a float by appending the suffix F (or f ).
Floating-point literals can also be specified in scientific notation, where E (or e ) stands for Exponent . For example, the double literal 194.9E-2 in scientific notation is interpreted as 194.9*10 -2 (i.e., 1.949 ).
Examples of double Literals
0.0 0.0d 0D 0.49 .49 .49D 49.0 49. 49D 4.9E+1 4.9E+1D 4.9e1d 4900e-2 .49E2
Examples of float Literals
0.0F 0f 0.49F .49F 49.0F 49.F 49F 4.9E+1F 4900e-2f .49E2F
Note that the decimal point and the exponent are optional and that at least one digit must be specified.
The primitive data type boolean represents the truth-values true or false that are denoted by the reserved literals true or false , respectively.
A character literal is quoted in single-quotes ( ' ). All character literals have the primitive data type char .
Characters in Java are represented by the 16-bit Unicode character set, which subsumes the 8-bit ISO-Latin-1 and the 7-bit ASCII characters. In Table 2.6, note that digits (
Table 2.6. Examples of Unicode Values
define special character values as shown in Table 2.7. These escape sequences can be single-quoted to define character literals. For example, the character literals
are equivalent. However, the character literals '
should not be used to represent newline and
Table 2.7. Escape Sequences
We can also use the escape sequence \ ddd to specify a character literal by octal value, where each digit d can be any octal digit ( “ 7 ), as shown in Table 2.8. The number of digits must be three or fewer, and the octal value cannot exceed \377 , that is, only the first 256 characters can be specified with this notation.
Table 2.8. Examples of Escape Sequence \ddd
A string literal is a sequence of characters, which must be quoted in quotation marks and which must occur on a single line. All string literal are objects of the class String (see Section 10.5, p. 407).
Escape sequences as well as Unicode values can appear in string literals:
"Here comes a tab.\t And here comes another one\u0009! (1) "What's on the menu?" (2) "\"String literals are double-quoted.\"" (3) "Left!\nRight!" (4)
In (1), the tab character is specified using the escape sequence and the Unicode value respectively. In (2), the single apostrophe need not be escaped in strings, but it would be if specified as a character literal(
). In (3), the double
Here comes a tab. And here comes another one ! What's on the menu? "String literals are double-quoted." Left! Right!
One should also use the string literals "\n" and "\r" , respectively, for correct interpretation of the characters " \u000a" and " \u000d" in the source code.
A white space is a sequence of spaces, tabs, form feeds, and line terminator characters in a Java source file. Line terminators can be newline, carriage return, or carriage return-newline sequence.
A Java program is a
White space aids not only in separating tokens, but also in formatting the program so that it is easy for
A program can be documented by inserting comments at relevant places. These comments are for documentation purposes and are ignored by the compiler.
Java provides three types of comments to document a program:
All characters after the comment-start sequence
through to the end of the line
// This comment ends at the end of this line. int age; // From comment-start sequence to the end of the line is a comment.
A multiple-line comment , as the name suggests, can span several lines. Such a comment starts with /* and ends with */ .
/* A comment on several lines. */
The comment-start sequences ( // , /* , /** ) are not treated differently from other characters when occurring within comments, and are thus ignored. This means trying to nest multiple-line comments will result in compile time error:
/* Formula for alchemy. gold = wizard.makeGold(stone); /* But it only works on Sundays. */ */
The second occurrence of the comment-start sequence /* is ignored. The last occurrence of the sequence */ in the code is now unmatched, resulting in a syntax error.
A documentation comment is a special-purpose comment that when placed before class or class member declarations can be extracted and used by the javadoc tool to generate HTML documentation for the program. Documentation comments are usually placed in front of classes, interfaces, methods and field definitions. Groups of special tags can be used inside a documentation comment to provide more specific information. Such a comment starts with /** and ends with */ :
/** * This class implements a gizmo. * @author K.A.M. * @version 2.0 */
For details on the javadoc tool, see the documentation for the tools in the Java 2 SDK.