Your code is written in a series
of statements, which can be organized into
blocks. These statements contain data and
operators, which are components of expressions.
You can annotate your code using comments, which
makes the code more understandable.
Any line of code before a
semicolon is known as a statement and is
executed by the Java interpreter when it hits
the semicolon; after executing that statement,
the interpreter moves to the next statement.
Each statement contains instructions for using
data and operators.
An expression is a part
of a statement that uses data and operators to
return a value.
A block is a collection
of statements enclosed in curly braces. Any
variables that you declare and that assign
values within a block are erased when the flow
of execution leaves the block. The block in
which the variable's value exists is called the
scope of the variable.
Comments are used to annotate
the code so that a reader can understand the
purpose of certain lines and blocks of code.
Comments are ignored by the Java compiler.
Multiline comments are preceded by /*
and are ended with */. Single line
comments are preceded by //. The double
slash "comments out" text only to the end of a
line. A comment would appear as follows:
/* This declares the
length variable for the Rectangle class */
length; //Declares the length variable for
the Rectangle class
To represent data values in
your code you use literals. Literals are
described by types, named by identifiers, and
stored in variables.
When you use literals in your
code, they appear in their raw form rather than
as a result of an expression. Several types of
literals are commonly used: numbers, integers,
floating points, characters, Booleans, and
Table 1.1 outlines Java's
strict definitions of these data types.
Table 1.1. Rules for Java
integer, floating point, or character.
decimal, hexadecimal, or octal.
integers between -128 and 127.
integers between -32768 and 32767.
integers between -2147483648 and 2147483647.
integers between -9223372036854775808 and
9223372036854775807 or have L or l appended
0x or 0X.
with a decimal point. Can be made
exponential by appending an e or E, followed
by the exponent.
integers represented by a single character
and enclosed in single quotes.
||In Java, the
Unicode character map is used. The following
special characters must be represented by
||Can only be
true or false. Are not
represented by 0 or 1.
||Zero or more
characters enclosed in double quotes.
Literals are described by
identifiers. Identifiers are sequences of
letters and digits, and can also be used to
describe variables, methods, and classes.
Identifiers can consist of any letter from a
to z, underscore, dollar sign, digits
from 0 to 9 (except as the first character);
identifiers are case-sensitive.
Java has several reserved
keywords that are its own identifiers, which
cannot be used as identifiers in any way other
than that defined by Java, as listed in Table
1.2. Though these words are reserved, not all
are used in the most recent release.
Table 1.2. Reserved
Operators are used to compare
values. Java has strict definitions of
operators. It doesn't allow for overloading,
which is a C developer's common practice of
changing the behavior of operators.
Java provides two types of
operators: binary and unary. Binary operators
are used to compare two values. Unary operators
use a single value, for example:
a >= b
The first example uses a
binary operator, >=, which compares
variables a and b. The second
is a unary operator, ++, which
increments the value of a by one.
All of Java's binary and unary
operators can be found in Table 1.3. They are
organized according to the precedence with which
they are performed.
Table 1.3. Binary and unary
overriding decimal, parentheses, brackets
~, ++, --
negation, bitwise complement, increment,
Multiplication, division, modulus
subtraction or unary negation
right shift, zero-fill right shift
<=, >, >=
less than or equal to, greater than, greater
than or equal to
not equal to
AND, Logical AND
OR, Logical OR
+=, -=, *=,
/=, %=, &=, ^=,
|=, <<=, >>=
There are four types of
statements to use for variables: declarations,
assignments, initializers, and casts.
You must always declare
variables before you can use them in your Java
program. Variable declarations assign
data types to variables. A declaration statement
in Java consists of a data type followed by an
identifier. Any of the data types listed in the
previous table can be used to declare variables,
int x, y, z;
are statements that assign values to variables.
These, like declarations, are required before
variables can be used. They are called by
setting an identifier equal to a value. This
value, of course, must be compatible with the
data type assigned to the variable identifier.
Initializers are assignment statements that are
combined with the declaration statement, for
Boolean IsReady = false
float miles = 3.62
short pages = 240
If you want certain variable
values to remain constant in your code, you can
use the final keyword, which ensures
that the variable cannot be changed by the code.
Its form is this:
final int pages = 500
are statements you use if you need to place a
value of one type into a variable of another
type. In C++, automatic coercion allows you to
do this without declaring that you were aware of
this change. In Java, you must explicitly call
such an instruction with a cast statement. Cast
statements are generally called as follows:
identifier = (datatype)
Java allows casts only between
compatible data types.
In this section, you have
learned about the data and operators that are
used in expressions that are parts of
statements. You now understand that statements
are organized with blocks and annotated with
comments. You have also examined some basic
statements that deal with variable declarations.
These are the fundamental elements of Java