Thursday, November 13, 2014

Variables and How To Use 'em

We've touched upon variables here and there in the previous few posts, and it occurred to me that I should probably clarify their purpose and use.

Programs would be remarkably useless if all you could do was hard code actual values into them.  So what we do is use symbolic representations of numbers, characters, words, etc.  This is similar to algebra, in that we often use 'x', 'y' and other such letters to represent numbers.  They act like placeholders.  Maybe it's better to think of a variable as a shoe box.  You can keep something in the box, and put a label on the box, too.

Let's start off with some very basic examples:

int one = 1;

No, you probably won't use 'one' as a variable name very often, but it's valid Java to do so.  We've created a label called "one" and assigned it the numeric value 1 in one fell swoop (or swell foop, if you prefer).  So we have a shoe box, with a label on it that says "one", and that has the number 1 inside of it.

Now here's why 'one' is a bad variable name:

one = one + 1;

Now one is equal to 2, and we've got the beginning of a comedy routine.

We can perform all kinds of basic math operations on integer variables this same way.

one = one * 2;

one = one / 2;

one = one - 1;

Assuming that all five lines of code above were run in a program, what would one now equal?

Well, it started out as 1, then we added 1 so it became 2.  Then we multiplied by 2 to get 4, divided by 2 to get 2, and then subtracted 1 to get back to 1.

Following chained sequences of events is an important skill for the programmer.  You need to be able to understand what each line of code will do to each variable and keep track of these things as you essentially simulate the program's run in your mind.  This can get very involved, and there's no shame in writing down the state of each variable after each step in a table format.

Of course, 'int' is just one data type, and a limited one at that.  It can't represent decimals easily for instance, and it's fairly hopeless at representing a word processing document.  But it's a start.  I don't intend to provide a complete reference to all data types here, but some basic examples include:

int - Represents an integer (whole number). Examples include 5, 18 and 42
double - Represents a double precision decimal number.  5.1842 could be a double
boolean - Represents a true or false value, which is really useful for conditionals and loops.  Must be either true or false

All of these types share the distinction that they are actual basic data types (atomic types), not objects.  There are objects in the Java library which can be used (fairly interchangeably) in their places.  The equivalent object representations for int, double and boolean are Integer, Double and Boolean.  We'll discuss those in another post, but for now I just want to point out that as a general rule (and you should follow it) object types will begin with an upper case letter, and that will help you to know what you're dealing with.  Of course, over time you'll get very used to the limited menu of atomic types and recognize them quickly.

 There are many mathematical operations one can perform with these data types, some to change them, some to test them.  We addressed some basic arithmetic operators above.

Sometimes you need to test variables for their values.  You can determine equality, or which number is greater by using comparison operators.  The first comparison operator you probably want to know about is equality.  We use a double equal sign for this (==).  This must not be confused with the assignment operator (=)!  Many logical errors in programs have cropped up from confusing these two, and they can sometimes be hard to find.  When quickly scanning through a couple of pages of code, the difference between:

if (n = 5)


if (n == 5)

can be hard to spot.  A trick that works surprisingly well for some developers is turning this around.  Basically, "if (5 == n)" is precisely equivalent to "if (n == 5)", but writing "if (5 = n)" is an error the compiler can catch because you cant assign a variable to a constant, only the other way around.  This is sometimes referred to as Yoda style or Yoda syntax.  Interesting is it not?

Technically, these comparison operators wind up evaluating to boolean values.  So it's perfectly OK to do something like this:

boolean nEquals5 = (5 == n);

We will go into booleans in more depth at another time, but for now I just want to point out that you can combine booleans with AND, OR and NOT operators (&&, ||, !) to evaluate more complex questions.

Hopefully, that's enough to keep you from getting too confused as we get into some examples going forward.  If you have questions, please feel free to post them, and I'll try to answer as quickly as I can.  The areas where people have difficulty are where I want to concentrate.

View code here