Lecture Date: Monday, April 6
How do you rememember which number is your student ID, which is your phone number, which is your social security number, and so on? Suppose you wrote down a number and are trying to remember what type of number it is?
Some people might file the numbers in particular locations: “it’s the number on the post-it by my bed so it must be my SSN.” We’ll call those people “static” since their filing system depends on things staying where they are placed. Others might file them wherever, but write down what they are: “this card says ‘SSN:’ so it must be my SSN.” We’ll call those people “dynamic” since their filing system works even if they move things around.
Computers have this same problem:
they store things in binary, but that’s just another kind of number.
Was this bunch of 1s and 0s supposed to be an
int or a
double or maybe even where to look to find an
We know it is a binary value, but what type of value is it?
Computers use one of the same two kinds of solutions to this problem too:
statically-typed languages remember what type they put in that box there
while dynamically-typed languages store the type with the value.
Java is statically typed.
When we write
int x we tell java “this box I’m calling
x, any bits you find in it must be an
Where the value is located tells us what type of value it is.
In those languages, variables don’t have type, values do.
We can write
x = 3 on one line and
x = "hi" on the next:
x is just a box, the type is stored with the value.
Today we’ll poke around a bit with these ideas, see how sometimes Java actually does both static and dynamic typing, and talk about learning other languages.
Java is also a strongly typed language, which means that once you set a type, it can never be changes. You can cast it, but that’s actually makes a new value with the new type, it doesn’t change the type of what was alreayd there. The original data stays with the original type.
As we talk about type systems and languages, we’ll think about how they impact the following:
- ease of writing programs
- program runtime speed
- liklihood of bugs
- ease of reading the program a month from now
- ability to run the program on a particular device