Summary
Subtype
Primitive types
- stores a copy of its value
kind | type | size(bits) | value range |
---|---|---|---|
booleans | boolean | 1 | |
strings | char | 16 | |
integral | byte | 8 | |
short | 16 | ||
int | 32 | ||
long | 64 | ||
floating-point | float | 32 | up to 38-digit, but not every number |
double | 64 |
java
int i = 5;
int j = i;
// both i and j store their own copy of the value 5
Reference types
- stores a pointer to the object in memory
- is a reference the instance of the object
java
Object i = new Object();
Object j = i;
// both i and j store the same pointer to the intance of object in memory
Concept
Dynamically typed languages
- Same variable can hold data of any data type
Statically typed languages
- Variable can hold data of the type the variable was declared with and its subtypes
Strong typed
- Type-safety, type-checking to reduce runtime-errors
Weak typed
- No type-checking
Rules of subtype notation
Widening and narrowing
- Java allows for a variable of type T to hold a value fron type S, if S<:T
Application
Java primitive type hierachy
Compile time type checking
java
double d = 5.0;
int i = 5;
d = i; // ok, since int is a subtype of double
i = d; // error