CodeGym /Courses /JAVA 25 SELF /Introduction to variables

Introduction to variables

JAVA 25 SELF
Level 1, Lesson 3
Available

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;
Variable declaration

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
int a;
We create a variable named a of type int.
String s;
We create a variable named s of type String.
double c;
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;
Assignment operation

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

img

How to catch a cat:

  1. Take an empty box.
  2. 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.

1
Task
JAVA 25 SELF, level 1, lesson 3
Locked
Current Year 📅
Current Year 📅
1
Task
JAVA 25 SELF, level 1, lesson 3
Locked
Personal message 💌
Personal message 💌
1
Task
JAVA 25 SELF, level 1, lesson 3
Locked
Project age 💻
Project age 💻
Comments (15)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Shanmugam E Level 2, CodeGym University in India, India
20 June 2026
Excellent teaching, easy to follow;
Muksud Alam Level 4, Bangladesh, Bangladesh
5 June 2026
the path of learning is one of the best paths for me, easy step by step, step by step t h a n k s
Darkstalker Level 2, CodeGym University in India, India
28 May 2026
It if getting more and more fun!!! /* umm.. excuseee me but it's time to code... Hmppp... ...*/
Man scortty Level 2, Accra, Ghana
25 May 2026
wow
Carlos CuAran Level 1, Colombia
20 May 2026
Buena leccion gracias
Landon Keith Level 4, United States of America, United States
30 April 2026
W's in za chat.
coolJavaPlayer Level 3, java land, Canada
21 March 2026
swing
Anonymous #11769142 Level 1, Ambasamudram, India
5 March 2026
nice
Anonymous #11766804 Level 4, Sydney, Australia
2 March 2026
nice i love it❤️
Md Ismail Level 2, Bangladesh, Bangladesh
10 January 2026
Best Teaching Method