Java Reference
In-Depth Information
Some general
remarks on Java
B
This appendix addresses some problems that frequently appear when one begins to
program larger applications in Java.
B.1
Objects, non-objects and references
Java is an object-oriented language. This does not mean that every entity defined
in Java is an object. Some basic entities such as integers ( int ), doubles ( double )
or characters ( char ) are not objects. Most of the time it does not matter whether
we deal with an object or a basic entity. There are, however, situations where a
seemingly equal treatment of objects and non-objects results in essentially differ-
ent results. These situations occur especially when fields inside an object can be
changed.
The fundamental difference between objects and non-objects is that the vari-
able (name) for a non-object is always linked to the fixed memory position where
this object is created (using the new -statement). The name stands for a value. The
variable name given to an object is a reference to some memory position. At cre-
ation (using the new -statement) it refers to the memory position where the object
is created. Later the memory position to which the variable refers can be changed
by an assignment to the variable.
Let us look at an example: We create two integers a and b (non-objects) and
assign values to them ( a=3 and b=4 ). We then assign b to a ( a=b ) and finally
assign a new value to b . After every assignment we print the current values. Below
is a listing of the code and output.
Code snippet:
inta=3;
intb=4;
a=b;
b=7;
Listing of output:
Valuea=3
Valueb=4
Search WWH ::




Custom Search