Summary

Subtype

Primitive types

  • stores a copy of its value
kindtypesize(bits)value range
booleansboolean1
stringschar16
integralbyte8
short16
int32
long64
floating-pointfloat32up to 38-digit, but not every number
double64
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