Like any computer language, Java consists of a set of basic set
of elements, such as keywords and symbols. It combines these elements
into small substructures such as expressions
and statements. These substructures
in turn create the larger structures called methods and classes.
Basic Elements
These essential elements of Java include:
- Keywords &
Symbols
The programmer has wide latitude for the names of variables and
classes but Java reserves some words for itself. These keywords
include, for example,
class,
float, return, if, else
See the Table of Keywords for
the complete list of reserved words and the Table
of Reserved Symbols
- Data types
Java represents different types of basic data and what operations
the data types can undergo. Eight keywords specify the eight kinds
of basic data, which are referred to as primitives. These
are
byte,
short, int, long -
integers
float, double -
floating point
boolean - logical true/false
char -
2 byte Unicode characters and
can also
act as 2 byte unsigned integers
Much more about primitives later.
- Operators
Java has arithmetic operators (+,
-, /, * ,%), boolean operators (&,
|, etc.), comparisons (==,
<=,
etc.) and others. A complete listing is given in the Tables
of Operators
- Identifiers
Identifiers are the names given to data, methods and classes.
Java is a strongly typed language, meaning that you must
declare the particular type to which a datum belongs. Identifiers
cannot begin with a number and an identifier cannot contain a
punctuation character or any character listed in the Reserved
Symbols Table. Underscore _ and the dollar sign $ are allowed.
The types of variables include:
- Primitive type variables
int
n = 5; //
n is an integer variable
double x; // x is a double type identifier
//
not explicitly initialized.
- Reference variables
- specify an object
String
x = "foo"; //
x references the string object.
- Array variables
x =
b * iArray[5]; //
element 5 in iArray is specified
- Literals
A specific primitive value in a line of code is called a literal.
These will be translated into values by the compiler and inserted
into the byte code.
int
i = 3; //
3 is an integer literal
double x = 4.0; // 4.0 is a double type literal
long l = 12L; // long literals must
be specified with L
float f = 1.2f; // float literals must be specified
with f
Language Structures
The above building blocks then combine into the following higher
level components to express instructions :
- Expressions
An expression contains an operation on one or more operands that
returns a value. In some cases it also changes an operand.
x
= 5 // assignment operation
y < 5 // comparison operation
14. * y // multiplication operation
- Statements
Composed of one or more expressions, a statement is a complete
action to execute:
x
= 5;
if (y < 5) x= 3;
return (x = 3.0 * (14.0 * y);
Finally, these components fit within the structure of a class
and its member fields and methods (methods are Java's
version of the subroutines and functions in other languages). We
will discuss Java classes and objects in Chapter
3 but for now you can program within the class framework without
understanding its details.
The generic program in the following section
illustrates the basic structure of a Java program.
Latest update: Oct. 13, 2004
|