Lecture Date: Friday, April 15
Once the program gets going, you have something called the stack and the heap.
Image credit: thenewcircle.com
The stack is where variable pointers go, such as with a complex/mutable type. The actual values of the complex/mutable type variable is found in the heap.
What happens when a variable no longer points to something in the heap? This is called garbage collection.
Let’s look at some examples at this tutorial: http://www.pythontutor.com/ We talked about this a bit at the beginning of the semester when we discussed activation records. We’ll revisit that a bit here with some more information.
How exactly does Python keep up with what type a variable is? How do other languages do it?
The War between Filing Systems
There are lots of numbers in your life. Social security numbers, student ID numbers, SIS person numbers, telephone numbers, zip codes, … How do you know which number is which?
Option 1: remember. This number is on the post-it beside my bed, which is where I wrote my student ID number, so that must be what it is.
Option 2: annotate. This piece of paper says SSN: 012345678 so it must by my SSN, not my telephone number.
In programming we call the first static typing and the latter dynamic typing. People argue about which one is better heatedly.
Python happens to be dynamically typed. Java, on the other hand, is statically typed.
How does this affect how we write our programs?
Imagine a program that you might write that has these lines:
1 2 3
In Python, this works just fine, because of the dynamic typing. But in Java, this would never work, because we would have defined
x to have a particular type. After that, it can’t be changed. But, if we store the type with the data itself and not the variable, then this works just fine.
x is just a pointer to some data - how we interpret it is up to the programmer.
You might think that dynamic typing can get a programmer in a lot of trouble if they don’t keep their variables straight. And you’re right. But it does make for less coding in general, even if dynamically typed languages are usually slower than statically typed ones.
Java is also a strongly typed language, which means that once you set a type, it can never be changes. Oh sure, you can cast it, but that’s just the type being temporarily converted. The original data stays with the original type.
So, if we’re building a program, and given a choice, which would you prefer to do?
- Write a program quickly
- Write a program that runs fast
- Write a program that has few if any bugs
- Write a program that you can read and make sense of latter
- Write a program that runs on many different devices
This partially drives our choice of language.