Introduction to Types
Introduction
A Rich Type Model
- Ada has a richer type model than Java
- Ada has:
- Stronger type checking that Java, ...
- which is stronger than C++, ...
- which is stornger than C
- More ways to create new types
- More ways to create subtypes
- Ability to create either primitive or reference types
Kinds of New Types
- Kinds of new types that can be created:
- Numeric:
- Integer
- Modular
- Floating point
- Fixed point
- Enumeration
- Object types (as in OO programming)
- Can be created with and without references!
- We skip these for now
Primitive and Reference Types
- Java has 2 kinds of types:
- Primitive:
- Variable of type T holds a value of type T
- Examples: int, double, boolean, char, etc
- Reference:
- Variable of type T holds a reference to a value of class T
- Examples: String, Object, Scanner, Person, Student, etc
- In Java the only way to create a new type is to create a new class
- Thus, all new types are reference types
- In Ada, new types can be either primitive or reference
- primitive (ie variable holds value)
- reference (ie variable holds pointer to value)
Why Primitive and Reference Types
- Why have primitive types?
- Answer: Two reasons discussed in class
- Why not have all primitive types: Consider this example:
Person p = new Person("Jack");
Student s = new Student("Jill", 11111);
Person q = s;
Consider the sizes of p, s, q, and the objects they reference
Does this Compile
- In the example from above, consider these assignments
Person p = new Person("Jack");
Student s = new Student("Jill", 11111);
Person q = s; -- Does this compile?
Student t = q; -- Does this compile?
If either of the above don't compile, how to fix?
- What change does the fix cause?
Motivation for Type Model
- Stronger type checking catches errors earlier
- More able to write at level of problem rather than level of machine