Unit 2. Introducing types
Nearly every programming language supports some idea of types. Types are important because they define the kinds of computation allowed on certain data. Take, for example, the text "hello" and the number 6. Say you want to do something like add them together:
Even someone with no prior programming experience would find this question interesting, because it’s not clear what to do. The two most obvious answers are to
- Throw an error
- Combine these values in the most reasonable way: "hello6"
To arrive at either option, you need a way to keep track of the type of your data, as well as the type of data your computation is expecting. Typically, we call the value of "hello" a String, and the value of 6 an Int. Regardless of your choice of programming language, you need to know the types you’re dealing with so you can either throw an error when they don’t match or do some sort of automatic conversion by knowing how to transform types. Even if you don’t think about types much in your programming language of choice, they are an important part of programming.