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.
These essential elements of Java include:
- Keywords &
The programmer has wide latitude for the names of variables and
classes but Java reserves some words for itself. These keywords
include, for example,
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
short, int, long -
float, double -
boolean - logical true/false
2 byte Unicode characters and
act as 2 byte unsigned integers
Much more about primitives later.
Java has arithmetic operators (+,
-, /, * ,%), boolean operators (&,
|, etc.), comparisons (==,
etc.) and others. A complete listing is given in the Tables
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
n = 5; //
n is an integer variable
double x; // x is a double type identifier
not explicitly initialized.
- Reference variables
- specify an object
x = "foo"; //
x references the string object.
- Array variables
b * iArray; //
element 5 in iArray is specified
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.
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
The above building blocks then combine into the following higher
level components to express instructions :
An expression contains an operation on one or more operands that
returns a value. In some cases it also changes an operand.
= 5 // assignment operation
y < 5 // comparison operation
14. * y // multiplication operation
Composed of one or more expressions, a statement is a complete
action to execute:
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