In order to store data in a computer, we have to know the different kinds of data that are
out there. You've probably heard that everything that's stored in a computer is just zeros
and ones, and that's true, but it would be hard to program that way. So we group the
zeros and ones together into groups that we can understand, and that makes it easier to
program. The first category of data we're going to use are integral numbers. We call
those int in programming. So these are counting numbers like 1, 2, and 3. You can recognize
them because they don't have any decimal point. We'll also use numbers with decimal points.
Those are called double in programming. Int versus double is very important. So in mathematics
2 and 2.0 are the same thing. In computer science they're not. They're from different
categories. So you have to pay attention to that. There are two different ways to store
words. One is called char, and the other is called string. A char is a single character,
and a string is a group of characters together. To store data you have to decide what category
to use and give it a name so you can find it.
So we could say, for example, int textMessagesReceived. The reason I picked int is because text messages
are integral, that is, you receive 1 or 2 or 3. You don't receive 2.3 text messages.
The variable name textMessagesReceived is in what we call camel case because the top
of it looks like the humps on a camel. The trick to camel case is that you put the words
together without spaces and you make the first letter of subsequent words start with a capital
letter. That way you can read the words a little bit more easily. Now what this does
is sets aside one location in memory and gives it a special name. Only one thing can be stored
at this location at any time, so if you store something else there, you lose the thing that
was stored there initially. That can be frustrating in programming sometimes.
To put data in the computer, we use an assignment statement. With assignment statements, the
left-hand side and the right-hand side are different. On the left-hand side there's a
location. On the right-hand side there's a value. So if, for example, we said int textMessagesReceived
= 4; we would take the value 4 and store it in the text messages received location. Now
this looks like math, but it isn't. And this is a place where computer science and math
are really different. We could create another location by saying int phoneCalls = 5;. If
we wanted to take the value in phone calls and put it in text messages, we would say
int textMessagesReceived = phoneCalls;. This takes the value 5 and stores it in the location
textMessagesReceived. The data that was there previously is lost, and we can't get it back.
Once we have data stored in memory, we can manipulate it by performing mathematical operations.
When you do this, you have to pay attention to int and double because they really matter.
The common arithmetic operations are very similar. Plus and minus, for example, mean
addition and subtraction. Asterisk means multiplication, so that's a little bit different. The other
thing that's different is you always have to use asterisk. So you've probably seen in
your algebra classes things like 3 times X, which you write 3X, but you can't do that
in programming. You have to say 3*X. This is an example of the computer being very literal.
There is a twist on this however. The data type matters. When you add together two integral
values, you get an integral value. When you add together two doubles, you get a double.
You actually knew that from your arithmetic because you know if you add two things that
have fractional parts that you're probably going to get something with a fractional part.
You can add an int to a double too. What happens is the int gets promoted to a double, and
then it's added as if they were two doubles, so you end up with something that has a decimal
point in it. There are other things that we can do in programming too. We can interact
with users. We can read in data from the keyboard. We can write out data to the screen, and when
we put this all together, we'll be able to write simple programs. Start reading and doing
interactive exercises to learn more.