Primitive Variables

Introduction

In order for programs to do their job, they have to be able to store lots of information about various stuff. A program's memory consists of individual elements called variables to do just that.

Basically, think of the computer as consisting of lots of boxes. Inside each box, the computer is only allowed to store one piece of information. So, maybe one box can store only one whole number. Perhaps another box can only store a decimal number. Yet another box stores an entire sentence.

So, each of these boxes can only store one type of thing. That is they have a type. You can picture it as a box with the type written on the outside, and the actual information sitting inside the box. So in in the example below, we have a box that can store an integer, and the integer that it stores is the number 20.



Primitive Data Types

In Java, there are certain primitive types, which are types of data that can be stored by primitive variables. They are summarised here:
int
integers, or whole numbers, eg. 41, -7, 0
double
decimal numbers, eg. 3.14159, -0.5, 1.0
char
single characters enclosed by single quotes; any character on the keyboard, eg. 'c', 'F', '#'. '2'
boolean
true or false
String
any sentence enclosed by double quotes, eg. "i love computer science", "abc123! :)"
On each box inside the computer, we have to write on the outside what type of data can go inside that box. So, if we want a box to store a single character, we have to write char on the outside.



Declaring

In fact, when we want to use a new variable in our programs, besides telling the computer just what kind of data goes inside the box, we also have to give that box a name. This is so that we can come back to the variable, and call it by name so that the computer knows which variable we're talking about. So, maybe some boxes might look like this:

To write code to declare these variables, you just write the data type followed by the name of the variable, and then a semi-colon (;). For example:

int age;
double distance;
char firstLetter;
String userName;
By writing the above, we are saying to the computer that we want a new box called age, and that it can only store an int (integer). We can use this box later to store an int. No actual integer is inside at this point! We have simply declared the variable age, we have not yet used it or given it a value to store.

Initialising Variables

Now, it would be good if we could stuff things into the box. The way we do this is to write something like this:
age = 20;
If we wrote this in our program after having created, or declared a variable, as we did above, then the box would now look like this:

This is the first time we have put something into the box. That is, it is the first time the variable has been given a value. This action of giving it a value for the first time is called ‘initialising a variable’.

Often, as soon as a variable is declared, we want to give it a value immediately. So, the above example can be written all in one line:

int age = 20;
This gives the same result as above: a box labeled age, which can only hold data of type int, is created and immediately, the integer 20 is put inside it.


Assigning Variables

What if we decided to change the value inside the box? We would write in exactly the same manner as before:
age = 21;
No matter what is inside the box (even if it there is nothing), the data inside the box is thrown away and the new value that’s on the right hand side is stuffed into the box. This is called ‘assigning a variable a value’. In this case, we have assigned the variable age the value 21. Hence, our picture now becomes:

There is one thing we must say at this point. If we were to try to assign this variable the value 3.14159, the computer would now allow it. Why? Because the value 3.14159 is not an int, and so, the computer is not allowed to put this thing into the box. Obviously, if we were to try to put inside the box anything else that is not an integer, such as "cat", "number2" or even "five", then the computer still must reject our attempt at assigning a non-int to an int variable.

Another point. Now that the variable has been declared, there is no need to say int anymore. The computer now remembers that the box can only hold ints (because int has been written on the label of the box as shown above). So, if we want change the value of this variable, there is no need to mention int. It is only needed when you are creating the box, that is, declaring the variable. In fact, if you do say int again, then the computer will try to make a new variable or box, and this may result in an error.


Using Variables

To really understand the powerful things we can do with variables, we have to get the basics behind them. So here, we are only going to use variables in a simple way, just so that you get the idea. What we will do for now is just print them on screen.

If we want to print something on one line, such as "Hello" onto the screen, we write this:

System.out.println("Hello");
It is not important how this works. You just need to know that if you put something inside the brackets and the quotation marks (ie. the " ") in this command line, then exactly that will get printed to screen all on one line.

Now, if you tried to do exactly the same thing without using the quotation marks, the result is quite different. That is, if we now write the line:

System.out.println(Hello);
what the computer does now is it tries to look for a variable labeled Hello, get whatever’s inside, and prints that value on the screen. But since there is no such variable labeled Hello, the computer will not allow this action. So, if we had the line:
System.out.println("age");
you would see this on the screen:
age
But if we had the line:
System.out.println(age);
then this would appear on the screen:
21
because the value 21 has been assigned to the variable that is labeled age. Note that after the value of the variable labeled age has been accessed, the value does not change. So, after printing the value of age (21), there is still a 21 inside the box labeled age.

Now, we know how to print the value of a variable. What if we wanted to change the value of a variable depending on its old value? For example, say we just want to increase the value of age by 1, no matter what its old value is. So, if age is 3, it should change to 4. If age is 21, then it should now become 22. The way this is done is this:

age = age + 1;
What has happened here? We are trying to assign the value of age something. That something is age plus 1. So what the computer says is "OK, I have to get the value of the variable called age. Oh, here it is. I get a 21. Now, what’s 21 plus 1? 22! OK, now, I must stuff 22 into the variable called age.". Now, note that we actually did the right hand side of the command line first. This is because we cannot put something into the box until we have worked out what that something is. This something must be an int. The computer has to work out what age + 1 is first before it can work out whether it is an int or not. After it works out that it is 22, an int, it then puts it into the box called age.


Strings

This type of variable is a very special type that can sometimes behave quite differently to the other types of variables. That is because it is not actually a primitive variable, it is another type of variable that we will learn later. But first, it is important to know two things:

Summary