1. Variables and boxes
Variables are special things for storing data. Any data. All data in Java is stored using variables. A variable is most like a box in meaning: a regular box.
Suppose you wrote the number 13 on a sheet of paper and put that sheet into 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 kind of values/data that can be stored in it. In a cake box we keep a cake, in a shoe box — shoes, and so on.
The value is an object, data, or information stored in the variable.
Every object in Java has its own type. For example, there can be the following data types: integer, floating-point number, text, type Cat, type House, and so on.
Every variable (box) also has its own type. A variable can store only values of the same type as itself. Different boxes are used to store different things: a box of chocolates, a carton for a dozen eggs, etc. Just like in real life.
2. Creating a variable
In Java, to create a variable, you use a command of the form:
type name;
Where type is the type of the variable (it matches the type of values the variable can store). And name is the name of the variable.
Examples:
| Creating a variable: first the type, then the name. | Description |
|---|---|
|
We create a variable named a of type int. |
|
We create a variable named s of type String. |
|
We create a variable named c of type double. |
The two most commonly used types are integers (denoted by int) and text (denoted by String). The double type is also popular — it is for floating-point (real) numbers.
3. Assignment
As mentioned above, a variable has a name, a type, and a value. We’ve already covered the name and the type, but what about the value? How do you put a value into a variable?
There is a special operation for putting a value into a variable — the assignment operation. It copies the value from one variable to another. It does not move it — it copies it. Like a file on disk. The assignment looks like this:
name = value;
Where name is the variable’s name, and value is the value that will be stored in the variable. As a value you can use a literal, the name of another variable, or even some expression involving variables.
Examples:
int i; // Variable i is created
int a, b; // Variables a and b are created
int x; // Variable x is created
i = 3; // The value 3 is stored in variable i.
a = 1; // The value 1 is stored in variable a.
b = a + 1; // The value 2 is stored in variable b.
x = 3; // The value 3 is stored in variable x.
x = x + 1; // In this line, x is increased by 1; x is now 4.
The equals sign
The assignment operation uses the equals sign =. This is not comparison, not equality. It is specifically a command to copy the value from the right of the equals sign into the variable on the left. For comparison in Java, a double equals == is used.
4. Variable name: what is allowed and what is not
Here are some facts that are useful to know when creating variables:
- A variable name can be anything (for example, temperature, score, userAge).
- But there are restrictions — it cannot start with a digit and cannot coincide with Java keywords (for example, you cannot name a variable int or if).
- It cannot contain special characters other than $ and _.
- Names are case-sensitive (Age and age are different variables).
- The common style is camelCase: the first letter is lowercase, then each word starts with a capital letter (userAge, maxScore).
Examples of allowed names:
int x123 = 1;
String kitten;
double PI = 3.14;
String MAIN_PATH = "c:/";
Examples of forbidden names:
int 1first = 1; // a name cannot start with a digit
int number# = 25; // the # symbol cannot be used in a name
String name" = "John"; // quotes are not allowed in a name
double pi+e = 5.123; // a name cannot contain +
You’ll learn the rest in future lectures. And now a logic question: in how many steps can you catch a cat in a box?
5. Cats and boxes

How to catch a cat:
- Take an empty box.
- Wait.
Just kidding 🙂
Well, you might be able to stuff as many cats as you like into a box, but a variable can hold only one value. The next task will be related to that.
GO TO FULL VERSION