Page 4
Data
The three most important properties of all data are: a type, a
name, and a value. Data declaration is the way we ask
for a data with a specific type and a specific name. When we declare
data, the computer allocates a space in memory to hold that data's
value. There will be some predetermined initial value, depending upon its
type. Data assignment is how we set or change the value of the
data.
Other properties include the scope, which we will talk about later, and
the memory address where the value is stored (which is much more important
in C++, where a distinction is made between a pointer and a reference) For
rules on naming, see the naming
conventions link.
Type
In the same way that Classes are descriptions of (possibly several)
objects, types are descriptions of (possibly several) values. For
example, we could have the type 'fruit'. Possible values of 'fruit' could
be 'apple', 'pear', 'banana', and possibly 'tomato'. (I can never
remember that last one). Please visit this page on
data types for a Venn Diagram and an informative table.
The largest distinction between data types is that of objects
references and primitives. Object references are data that point
to actual objects. For example, a rollerskate would have four wheel objects
as data. The type of each of the data memebers of rollerskate is "Wheel
object reference". Each data members refers to a Wheel object. The default
value for object references is the JAVA keyword null. Null
represents a lack of an object. If a rollerskate lost one of its wheels,
then that data members would no longer point to a Wheel object. It would now
point to "null". Primitives can be either single-letter alphabetic,
numerical, or logical. These cover values such as 'a', 'b', 'c', 1, 2, 3,
true, and false. Note that multi-letter alphabetic, such as "abc" is
not a primitive value.
There are several numerical primitives. The difference between them is
whether they are integers or decimals, and also how large their range
is. For example, you cannot represent the number 130 with a data of
type byte. On the other hand, using a long might be excessive, though it
will get the job done. In general, we will use the type int for integers and
double for decimals.
JAVA is extremely type-sensitive. You could not, for example, choose to
assign an object reference data the value of a numerical data. In fact,
you cannot, in general, assign even between two numerical types! You
cannot also assign between object references that point to objects of
different classes. That would be like attempting to put a dog muzzle on a
bear. Even though they are both muzzles, they are still different types and
can't be used interchangeably.