Marc said:
I apologize for what must be an absolutely silly question, but:
What is the difference between class and type? I see books referring
to both interchangeably. I though that type meant 'primitive type' and
class for the 'reference types' like objects. Obviously I am
completely wrong.
Though the terms don't have universal definitions, that is not the
common usage within Java. Primitive and reference types are both types,
and "class" describes the actual form of an object (which is not a
variable at all).
The primary difference is that "type" is a compile-time concept. Except
for a brief interlude of bytecode verification and the reflection API
(which can be seen as echos of the source language from a compiler
standpoint, in a sense), it's lost in the actual application at runtime.
The class of an object, though, exists and is important at runtime,
primarily in polymorphism, and in the behavior of casting operations.
Perhaps this will make it clear. In the expression:
Object a = new Date();
String s = a.toString();
The *type* of the variable 'a' is Object (which is actually a type
notation in Java source code meaning a *reference* to an Object), but
the *class* of the object it points to is Date. The type (Object) only
affects what code is valid according to the compiler's type-checking,
but *not* what the code actually does. The class of the object affects
what the code does, so that the a.toString() call in the second line
returns a String that looks like a Date, not one that looks like
"java.lang.Object@XXXXXXXX".
HTH,
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation