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.
int
double
char
boolean
true or false
String
char on the outside.

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:
By writing the above, we are saying to the computer that we want a new box calledint age; double distance; char firstLetter; String userName;
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.
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:age = 20;

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:
This gives the same result as above: a box labeledint age = 20;
age, which can only hold data of type int, is created
and immediately, the integer 20 is put inside it.
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 variableage = 21;
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.
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:
what the computer does now is it tries to look for a variable labeledSystem.out.println(Hello);
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:
But if we had the line:age
then this would appear on the screen:System.out.println(age);
because the value21
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:
What has happened here? We are trying to assign the value ofage = age + 1;
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 must always have quotation marks when being given a value. This is because the
quotation marks mean ‘exactly this’. Without the quotation marks, the computer goes looking for a
variable that is labeled with that name. For example:
will print:String s1 = "Hello"; String s2 = "s1"; String s3 = s1; System.out.println(s1); System.out.println(s2); System.out.println(s3);
Hello s1 Hello
Strings are always considered to be just a series of any characters on the keyboard. This means
that we can have letters, numbers, punctuation marks, spaces, and any other symbol on the
keyboard. Hence, mathematical operations cannot be performed on Strings. So, if we had two
variables s1 and s2, both of type String, and they had values of "123" and "456", then, if we tried
using the ‘+’ operator on them, the Strings would just concatenate, that is, join up one behind the
other, to form 123456. That is,
will printString s1 = "123"; String s2 = "456"; String s3 = s1 + s2; System.out.println(s3);
123456
int, double,
boolean, and char.
int age;
age = 20;
If you wanted to actually print out the word "age" onto the screen, you would use quotes:System.out.println(age);
System.out.println("age");
age = age + 1