CS101: Variables

Before we dive into the meat ‘n potatoes of game development, it will be useful to review some of the basics. Let’s bring it back to CS101 and talk about variables. You’re probably pretty solid on all of this stuff, but it can’t hurt to be sure. You might learn something, you know?

Let’s hit the books and start with the dictionary entry.

variable

noun – something that changes or that can be changed : something that varies

mathematics : a quantity that can have any one of a set of values or a symbol that represents such a quantity

So a variable is just a symbol that represents a value. You probably remember variables from Algebra 1; y=mx+b and all that jazz. Back then, variables stood in as placeholders for numbers. In programming, variables function in much the same way. There’s one important difference; variables remember their value. So let’s hop into some Java and look at a variable declaration.

Pretty simple, but there’s a lot here. This isn’t just a declaration; it’s actually an initialization, too. What’s the difference? A declaration gives our variable a type and a name. An initialization is the first value we assign to our variable. In most languages, you can do both at the same time. For the most part, you always want to initialize your variables when you declare them. Many languages, Java included, have default values that variables initialize to when you don’t explicitly give them a value, but it’s clearer to do it yourself. Any time your code can give more information to whoever is reading it, including you, it’s a valuable inclusion. Let’s break this line down further.   int  is where we state our type (in this case, it’s an integer).  x  is our variable name.  99 is our initial value. The semicolon is part of Java’s syntax and signifies the end of an expression.

We’re getting into language specific stuff here, so from this point on, everything I’m talking about is specific to Java. Many languages work in almost the exact same way, including C++ and C#, so it’s still important stuff if Java isn’t your language of choice.

I mentioned that variables have types. Our variable up there was an integer. Java is a statically typed langauge, which means that every variable must have a type declared and its type cannot change. Java has a lot of variable types; here is a quick list of some of them with example initializations.

Those of you who have spent some time teaching themselves Java might be thinking, “Wait, you forgot about Strings and Objects!” I didn’t forget about them; they aren’t primitive types. A primitive type is the simplest kind of data a program can represent; it cannot be broken down into simpler values. Primitive types are also defined by the language itself. You can’t make new primitive types. Can you make new, non-primitive types? Of course! That’s a huge part of object oriented programming and we’ll get to it soon. One last important point about primitive types: primitive types are always passed by value. The difference between pass by value and pass by reference will be covered when we dig into methods. Our next topic is the blueprint for composite types: classes.

Next post: Classes

Comments

Leave a Reply

Your email address will not be published / Required fields are marked *