1. Variables and boxes

Variables are special things for storing data. Any data. All data in Java is stored using variables. One of the best ways to conceive of a variable is as a box: a completely ordinary box.

For example, let's say that you write the number 13 on a piece of paper and put it in a box. Now we can say that "the box stores the value 13".

Every variable in Java has three important properties: type, name, and value.

The name is used to distinguish one variable from another. It's like a label on a box.

The type of a variable determines the type of values/data that can be stored in it. We store a cake in a cake box, shoes in a shoe box, etc.

The value is some object or the data stored in the variable.

Every object in the Java language has its own type. For example, we can have the following data types: integer, fractional number, text, Cat, House, etc.

Each variable (box) also has its own type. A variable can only store values that correspond to its type. Different boxes are used to store different things: a box of chocolates, a carton for a dozen eggs, etc. It's just like in real life.

2. Creating a variable

In the Java language, we create a variable using a command that takes this form:

type name;
Declaring a variable

where type is the type of the variable (which corresponds to the type of the values that the variable can store), and name is the name of the variable.


Creating a variable: first the type, then the name. Description
int a;
Create a variable named a whose type is int.
String s;
Create a variable named s whose type is String.
double c;
Create a variable named c whose type is double.

The two types most commonly used are integers (denoted by int) and text (denoted by String). The double type is also popular. It represents fractional (real) numbers.

3. Assignment

As mentioned above, a variable has a name, type, and value. We already considered the name and type, but what about the value? How do I put a value into a variable?

To assign a value to a variable, we have the assignment operator. It copies a value from one variable to another. It does not move the value. It copies. Like a file on disk. Assignment looks like this:

name = value;
Assignment operator

where name is the name of the variable and value is the value that will be put into the variable. The value can be a literal value, the name of another variable, or even some expression that includes variables.


Code Description
int i;
int a, b;
int x;
The i variable is created
The a and b variables are created
A x variable is created
i = 3;
The i variable is set to the value 3.
a = 1;
b = a + 1;
The a variable is set to the value 1.
The b variable is set to the value 2.
x = 3;
x = x + 1;
The x variable is set to the value 3.
On the next line, the value of x is increased by 1. x is now 4.

The assignment operator is the = symbol. This is not a comparison. It is nothing more or less than the command to copy the value to the right of the equals sign into the variable, which is on the left. For a comparison operation, Java uses double equals: ==.

4. Cats and boxes

How to catch a cat:

  1. Take an empty box.
  2. Wait.

That's a joke 🙂

Of course, you may be able to fit a dozen cats into a box, but only one value can be put into a variable. This is related to next task.

New Java Syntax, level 1, lesson 3
It's cool to be a programmer!
Write a program that displays: "I think being a programmer is cool". Example output: I think being a programmer is cool