Java Reference
In-Depth Information
Reference
variables refer to an object (see Chapter 3):
Vector vec
=
new Vector (); // vec is a reference to a
// Vector object
Array
variables (see Chapter 3):
x
=
b*c[5]; // c[5] refers to the element at index 5
// of array c
Literals
-Aspecific value in a line of code is called a literal:
double x
=
4.0; // 4.0 is the double floating-point
// literal
String str
="
Astring
"
;//
"
Astring
"
is a string literal
char c
='
c
'
;
//
'
c
'
is a character literal
2.2.2 Language structures
The basic language elements above combine to create higher-level structures that
perform the operations of a program. These structures include:
Expressions
-Anexpression contains an operation on one or more operands and returns
avalue:
x
=
5
- assignment operation
x
<
5
- comparison operation
14.*y
- multiplication operation
Statements
-Astatement, which can hold one or more expressions, presents a complete
action or set of actions to perform:
x
=
5;
if (y
<
5) x
=
3;
return 3.0*(14.*y);
Statements end with a semicolon.
Finally, these structures in turn make up the structure of a class and its fields
and methods (methods resemble subroutines or functions in other languages). We
fully develop the definition of Java classes in later chapters.
2.3 A simple application
The following example code illustrates the bare essentials of a Java application
program that prints a short text string to the console. You can see that the code
must always occur within the framework of a class. Follow the instructions in
Chapter 1 for compiling and executing this application.
Search WWH ::
Custom Search