Smallest individual units in a
program are known as Tokens. The complier recognized them
for building up expressions and
statements. Java languages include 5 types of Tokens. They
are:
# Reserved Keywords
# Identifiers
# Literals
# Operators
# Separators
Keywords:
A keyword is a word that has
special meaning defined by the Java programming language.
The program shown earlier in
Listing 1-1 uses four keywords:public, class, static, and void.
In all, Java has 51 keywords.
They’re listed in alphabetical order in Table 1-1. abstract do if
package synchronized boolean
double implements private this break else import protected
throw byte extends instanceof
public throws case false int return transient catch final
interface short true char finally long static try
class float native strictfp void
Identifiers:
An identifier is a word that you
make up to refer to a Java programming element by
name. Although you can assign
identifiers to many different types of Java elements, they’re
most commonly used for the
following elements:
Classes, such as the HelloApp
class in Listing 1-1
Methods, such as the main method
in Listing 1-1
Variables and fields, which hold
data used by your program
Parameters, which pass data
values to methods
Comments:
A comment is a bit of text that
provides explanations of your code. Comments are completely
ignored by the compiler, so you
can place any text you wish in a comment. Using plenty of
comments in your programs is a
good idea to explain what your program does and how it
works.
Java has three basic types of
comments:
end-of-line
comments
traditional
comment
Multi comments
JavaDoc
comments.
End-of-line
comments (single Line comments):
An end-of-line comment begins
with the sequence // and ends at the end of the line. You can
place an end-of-line comment at
the end of any line.
Everything you type after the //
is ignored by the compiler. For example:
total = total * discountPercent;
// calculate the discounted total
If you want, you can also place
end-of-line comments on separate lines,
like this:
// calculate the discounted total
total = total * discountPercent;
You can place end-of-line
comments in the middle of statements that span
two or more lines. For example:
total = (total * discountPercent)
// apply the discount first
+ salesTax; // then add the sales
tax
Traditional
comments (Multi Line comments):
A traditional comment begins with
the sequence /* and ends with the sequence */ and can
span multiple lines. For example:
/* HelloApp sample program.
This program demonstrates the
basic structure
that all Java programs must
follow. */
A traditional comment can begin
and end anywhere on a line. If you want, you can even
sandwich a comment between other
Java programming elements, like this:
x = (y + /* a strange place for a
comment */ 5) / z;
Usually, traditional comments
appear on separate lines. One common use for traditional
comments is to place a block of
comment lines at the beginning of a class to indicate
information about the class such
as what the class does, who wrote it, and so on. However,
that type of comment is usually
better coded as a JavaDoc comment, as described in the next
section.
You may be tempted to temporarily
comment out a range of lines by placing
/* in front of the first line in
the range and */ after the last line in the range.
However, that can get you in
trouble if the range of lines you try to comment
out includes a traditional
comment. That’s because traditional comments
can’t be nested. For example, the
following code won’t compile:
/*
int x, y, z;
y = 10;
z = 5;
x = (y + /* a strange place for a
comment */ 5) / z;
*/
Here, I tried to comment out a
range of lines that already included a traditional comment.
Unfortunately, the */ sequence
near the end of the fifth line is interpreted as the end of the
traditional comment that begins
in the first line. Then, when the compiler encounters the */
sequence in line 6, it generates
an error message.
Multi Comments
(Comments with comments or Nested comments):
Here, comments with in comments,
nested comments. It can be represented with
/** Multi
comments or Comments with in comments or Nested comments */
JavaDoc
comments:
JavaDoc comments are actually a
special type of traditional comment that you can use to
automatically create Web-based
documentation for your programs. Because you’ll have a
better appreciation of JavaDoc
comments when you know more about object-oriented
programming, I devoted a section
in Book III, Chapter 8 to creating and using JavaDoc
comments.