Introduction to Programming: Data Types

Datatypes: What they are, and how they are used.

Data Types

No matter which language you use, you will most likely be dealing with different types of data. What is meant by this? Well, in the previous example we used a Number, a 5 to be exact, which is one type of data. In actuality, there are many types of data that we may need to use in our program depending on what it does, and most languages will have a concept of the three basic types, which can further be broken down into more specific types in some languages.

Numbers

As already discussed, a number is any value that represents a numeric value, such as the 5 that we used in our previous examples, or even decimal values, like 3.14. Depending on the language that you use, numbers might be further broken down into even more specific types, such as Integer and Float. Again, if you remember your definitions from mathematics, an Integer is a "Whole Number" that can be either negative or positive (or 0!). A Float would be in reference to a floating-point value, which is one with a decimal value, such as 3.14 or 12526.18369.

Languages with these types will likely have size limits on how big the number can be, due to memory storage concerns. For instance, an Integer might only be able to be a number between about -2 billion and +2 billion. If you needed to store larger numbers, you would need to use a different type denoted by a different name, which can depend on the language used. These concepts can seem confusing at first, but it basically just comes down to how we are able to store data in memory. You can read more about this in <link:Data Types in Memory>.

Boolean

A boolean is a simple type of data that you might recall from algebra and it has only two values, true or false. In some languages a boolean may actually just be a tiny number type that is either 1 (for true) or 0 (for false), but others abstract this into literal values "true" and "false" to make code nicer to read. Booleans are used frequently for condition logic, which is basically like "If this is true, then do this", which will be discussed more later. An example of storing a boolean value might look something like this (note we're using a boolean literal here):

let hasChocolate = true;

Strings

Another type of data could be a String, which if you haven't taken discrete mathematics yet, is just a sequence of characters strung together, such as "Hello world!", which has twelve characters (yes we include the space and exclamation point!).

Note: If you're wondering, "How does a computer store the letter 'a' in memory?", you can read up more about this in <link: Data Types in Memory>

In our previous example on output, we had printed out the number 5 to the console by first storing it in a variable and then using our special print() statement (again, we'll discuss functions more later, so don't worry if that seems confusing). We could also print other things out, such as the Strings we mentioned earlier:

let greeting = "Hello World!";
print(greeting);

This will print out the words 'Hello World!' to the console, however it will not print out the double quotation marks. The double quotes are simply how we have to write String literals in code (Remember, a literal is a value that is represented by an actual value, so like 5 is a numeric literal value and "hello" is a string literal). If we wanted to store the String "dog" in a variable and we were to just write:

let animal = dog;

The compiler would get confused thinking we meant we wanted to copy the value inside a variable called 'dog' to the variable 'animal'. The double quotes allow us to tell the compiler we are assigning a String literal, not grabbing the value from inside another variable called 'dog'. Thus the correct statement to do what we wanted is:

let animal = "dog";

Arrays

I know, I said three basic types, but we're throwing in a fourth that is a little different from the others. An array is basically just a sequence of values of a specific type. What does that mean? Well if we were to think of books on a book shelf, each book could be like one of our values. So one book represents a 5, maybe another book represents a 21, etc. Instead of our variable referencing just one of these values (books), we instead have a variable that is referencing the entire bookshelf.

Books on a bookshelf

So in essence, our variable has many values inside of it, not just one. They aren't just inside randomly though, they are in a very specific order, just like how our books on a bookshelf are in a very specific order, an order that we defined when we created it.

let values = [5, 21, 17, 4];

Here we can see that again we created a variable, called 'values', just as we have created variables before, but this time we're initializing it to a bit of a different looking thing. The square brackets ([ ]) represent that we are creating an array, and we may need to do this differently depending on which language we are using, but we'll keep it simple for this example. Next, inside the brackets, we have defined which values we want inside our array, and what order they are in. The first value is a 5, then a 21, then a 17, and then the last value of the array is a 4. There are a total of 4 values in this array.

Books on a bookshelf with index values

If we wanted the first value of the array, we could access it by using it's index location. The index location is how far from the beginning of the array it is, and the beginning of the array is the very first value. This means the first value is at index 0. Looking at our bookshelf example again, if we had 4 books on the shelf, that would be like having an array of size 4. The first book has the index location of 0, because it is 0 books from the beginning (it has 0 books before it). The second book has the index location of 1, because it is 1 book from the beginning (it only has 1 book before it). The third book has the index location of 2, because it is 2 books from the beginning (it has 2 other books before it). Lastly, the fourth book has the index location of 3, because it is 3 books from the beginning (it has 3 books before it).

This is a concept that can be very confusing to many people when they first start programming, as we would all think that the first book would be at index 1, but in programming, we always start counting from 0, not 1. I think it can make it easier if you think about it as described above, where the index relates to how many values are before it. Thinking that way, the first value has no other values before it, thus it's index is 0. Now to make this reference in code, most language are going to use syntax that looks like the following:

let values = [5, 21, 17, 4];
let a = values[0];

So what we have here is we created an array with our first statement, and then in our second statement, we copied the value that was at index 0 of the array and put it into the variable 'a'. Unlike when we copied the values from variables before, this time we are again using the square brackets ([ ]), but when used this way, it denotes that we don't want the entire array, just the value at that specific index location. So what value does 'a' now have? Think about it for a minute and see if you can guess. Have an answer? Well if you said 5 then you are correct, since 5 is the first value of the array (i.e. the value at index 0), that is what was copied into the variable 'a'. You'll notice an interesting thing when you look at how these arrays are used, as this array has 4 items in it (we would say it's "size" is 4), but to access the last element of the array, you would use values[3]. If you were to try to do values[4], you would not get what you wanted at best, and possibly get an error at worst, because there is no index 4. This is something that is a common cause for mistakes in early programming, and can even trip people up now and then that have been programming for a while.

Another quick note on arrays, if you wanted to say replace just one very specific value in the array with another, how would you do that? Well, first, lets define what we want to do. We have a bookshelf full of books, and I want to replace one very specific book, with a different book, thus changing one of the values in the array.

Repacing a book in a bookshelf

In this example, we replaced the second book, which was at index 1, with a different book. We can do this same thing, replacing one of the values in our array. How? Well we can specify which value we want to replace the same way we did when we wanted to copy it into another variable, but this time it will be on the left side of the assignment operator (the equals sign).

let values = [5, 21, 17, 4];
values[1] = 8;
print(values);

So again, in our first statement, we created a variable put an array of values in it. In the second statement, we are saying we want to assign a new value to the second value of the array (the value at index 1 is the second value, remember that can be tricky!), and that new value is an 8. So now, if we were to print out those values as in our third statement, it would print out [5, 8, 17, 4]. We have replaced the second value of the array, and the old value, 21, is now gone forever.

Data types can be a bit of a complex topic at first, but they are just different ways that we want to represent data, and become very important as you move forward to write more complex code. More articles to come!