Sie sind auf Seite 1von 4

2.

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.

Tokens are identifier, keyword, separator , operator , and literal .

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.

abstract continue goto package switch

assert default if private this

boolean do implements protected throw

break double import public throws

byte else instanceof return transient

case extends int short try

catch final interface static void

char finally long strictfp volatile

class float native super while

const for new synchronized

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).

3. Separators (also known as punctuators): punctuation characters and paired-delimiters . The


third category of token is a Separator (also known as a punctuator). There are exactly nine,
single character separators in Java, shown in the following simple EBNF rule.

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.

For example the Java code Math.max(count,limit); contains nine tokens

1. an identifier (Math), followed by


2. a separator (a period), followed by
3. another identifier (max), followed by
4. a separator (the left parenthesis delimiter), followed by
5. an identfier (count), followed by
6. a separator (a comma), followed by
7. another identifier(limit), followed by
8. a separator (the right parenthesis delimiter), followed by
9. a separator (a semicolon)

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)

o Numeric: int and double


o Logical: boolean
o Textual: char and String
o Reference: null

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.

Literals are integer-literal, floating-point-literal, boolean-literal,


character-literal, string-literal, and null-literal

Here are some examples of literals of each of these types.

Literal type

1 int

3.14 double (1. is a double too)

true boolean

'3' char ('P' and '+' are char too)

"CMU ID" String

null any reference type

2.14.

Command Line Arguments

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.

public class ReadArgs


{
public static final void main(String args[])
{
for (int i=0;i<args.length;++i)
{
System.out.println( args[i] );
}
}
}

Sample Run

With the following command line, the output shown is produced.

java ReadArgs zero one two three

Output:

The following command line arguments were passed:


arg[0]: zero
arg[1]: one
arg[2]: two
arg[3]: three

Das könnte Ihnen auch gefallen