Java Reference
In-Depth Information
j--primitive types:
int
- 32 bit two's complement integers
boolean
- taking the value
true
or
false
char
- 16 bit Unicode (but many systems deal only with the lower 8 bits)
j--reference types:
Arrays
Objects of a type described by a class declaration
Built-in objects
java.lang.Object
and
java.lang.String
j-- code may interact with classes from the Java library but it must be able to do so using
only these types.
4.2.2 Type Representation Problem
The question arises: How do we represent a type in our compiler? For example, how do we
represent the types
int
,
int[]
,
Factorial
,
String[][]
? The question must be asked in
light of two desires:
1. We want a simple, but extensible representation. We want no more complexity than is
necessary for representing all of the types in j-- and for representing any (Java) types
that we may add in exercises.
2. We want the ability to interact with the existing Java class libraries.
Two solutions come immediately to mind:
1. Java types are represented by objects of (Java) type
java.lang.Class
.
Class
is a
class defining the interface necessary for representing Java types. Because j-- is a
subset of Java, why not use
Class
objects to represent its types? Unfortunately, the
interface is not as convenient as we might like.
2. A home-grown representation may be simpler. One defines an abstract class (or
interface)
Type
,
and
concrete
sub-classes
(or
implementations)
PrimitiveType
,
ReferenceType
, and
ArrayType
.
4.2.3 Type Representation and Class Objects
Our solution is to define our own class
Type
for representing types, with a simple interface
but also encapsulating the
java.lang.Class
object that corresponds to the Java represen-
tation for that same type.
But the parser does not know anything about types. It knows neither what types have
been declared nor which types have been imported. For this reason we define two placeholder
type representations:
1.
TypeName
- for representing named types recognized by the parser like user-defined
classes or imported classes until such time as they may be resolved to their proper
Type
representation.
2.
ArrayTypeName
- for representing array types recognized by the parser like
String[]
,
until such time that they may resolved to their proper
Type
representation.
Search WWH ::
Custom Search