2.13 Java Tokens: Tokens Are Identifier, Keyword, Separator, Operator, and Literal
2.13 Java Tokens: Tokens Are Identifier, Keyword, Separator, Operator, and Literal
13
Java Tokens
In a Java program, all characters are grouped into symbols called tokens. Larger language
features are built from the first five categories of tokens (the sixth kind of token is recognized, but
is then discarded by the Java compiler from further processing). We must learn how to identify all
six kind of tokens that can appear in Java programs.
We will examine each of these kinds of tokens in more detail below. For now, we briefly describe
each token type.
1. Identifiers: These are the names the programmer chooses. These names can be assigned
to variables, methods, functions, classless, etc., to uniquely identify from a compiler. Java
keywords can not be used as identifiers.
2. Keywords: These are the names already in the programming language . The second
category of token is a Keyword, sometimes called a reserved word. Keywords are identifiers that
Java reserves for its own use. These identifiers have built-in meanings that cannot change. Thus,
programmers cannot use these identifiers for anything other than their built-in meanings.
Technically, Java classifies identifiers and keywords as separate categories of tokens.
The following is a list of all 49 Java keywords we will learn the meaning of many, but not all,of
them in this course. It would be an excellent idea to print this table, and then check off the
meaning of each keyword when we learn it; some keywords have multiple meanings, determined
by the context in which they are used.
Notice that all Java keywords contain only lower-case letters and are at least 2 characters long;
therefore, if we choose identifiers that are very short (one character) or that have at least one
upper-case letter in them, we will never have to worry about them clashing with (accidentally
being mistaken for) a keyword. Also note that in the Metrowerks IDE (if you use my color
preferences), keywords always appear in yellow (while identifiers, and many other tokens, appear
in white).
Separator <= ; | , | . | ( | ) | { | } | [ | ]
In the separator rule, the bracket/brace characters stand for themselves (not EBNF options or
repetitions).
Note that the first three separators are tokens that separate/punctuate other tokens. The last six
separators (3 pairs of 2 each) are also known as delimiters: wherever a left delimiter appears in a
correct Java program, its matching right delimiter appears soon afterwards (they always come in
matched pairs). Together, these each pair delimits some other entity.
4. Operators: symbols that operate on arguments and produce results. The fourth category of
token is an Operator. Java includes 37 operators that are listed in the table below; each of these
operators consist of 1, 2, or at most 3 special characters.
= > < ! ~ ? :
The keywords instanceof and new are also considered operators in Java. This double
classification can be a bit confusing; but by the time we discuss these operators, you'll know
enough about programmig to take them in stride.
It is important to understand that Java always tries to construct the longest token from the
characters that it is reading. So, >>= is read as one token, not as the three tokens > and > and =,
nor as the two tokens >> and =, nor even as the two tokens > and >=.
Of course, we can always use white space to force Java to recognize separate tokens of any
combination of these characters: writing > >= is the two tokens > and >=.
5. Literals (specified by their type)
The fifth, and most complicated category of tokens is the Literal. All values that we write in a
program are literals: each belongs to one of Java's four primitive types (int, double, boolean,
char) or belongs to the special reference type String. All primitive type names are keywords in
Java; the String reference type names a class in the standard Java library, which we will learn
much more about soon. A value (of any type) written in a Java program is called a literal; and,
each written literal belongs in (or is said to have) exactly one type.
Literal type
1 int
true boolean
2.14.
This class demonstrates how command line arguments are passed in Java. Arguments are
passed as a String array to the main method of a class. The first element (element 0) is the first
argument passed not the name of the class.
Program 2.4
//An example that prints in the command line arguments passed into the class when
//executed.
Sample Run
Output: