CodeGym /Courses /JAVA 25 SELF /Integers: int type, operations with int

Integers: int type, operations with int

JAVA 25 SELF
Level 2, Lesson 1
Available

1. Taking a closer look at the int type

If you want to store integers in variables, you need to use the int type.

int is short for Integer (meaning whole), which hints that this type stores whole numbers.

Variables of type int can store integers from -2 billion to +2 billion. Or, to be more precise, from -2,147,483,648 to 2,147,483,647.

Interesting fact

These non-round bounds are due to how computer memory is organized.

In Java, the int type uses 4 bytes of memory. Each byte consists of 8 bits. Each bit can take only 2 values — 0 or 1. An int contains 32 bits and can represent 4,294,967,296 values.

Half of that range is allocated to negative numbers, and the other half — to positive ones. That’s why the range is exactly from -2,147,483,648 to 2,147,483,647.

2. Creating a variable of type int

The int type is intended for storing integers. To create in code a variable capable of storing integers, use a statement like this:

int name;
Declaring a variable of type int

Where name is the variable name. Examples:

Command Description
int x;
An integer variable x is created
int count;
An integer variable count is created
int currentYear;
An integer variable currentYear is created

Letter case matters: the statements int color and int Color will declare two different variables.

The statements Int Color and INT COLOR won’t be understood by the compiler at all and will result in an error. int is a special word that denotes the integer type, and it is written only in lowercase.

3. Short form for creating variables

If you need to create many variables of the same type in one place in a program, you can use a shortened form:

int name1, name2, name3;
Short form for creating several variables of the same type

Examples:

Commands Short form of the same commands
int x;
int y;
int z;
int x, y, z;
int count;
int totalCount;
int count, totalCount;
int day;
int month;
int year;
int day, month, year;

4. Assigning values

To put a value into a variable of type int, use this statement:

name = value;
Assigning a value to a variable

Where the value can be any integer expression. Examples:

Command Note
int a;
a = 5;
int b;
b = 2*1000*1000*1000;
The variable will hold “2 billion”
int c;
c = -10000000;
The variable will hold “minus 10 million”
int d;
d = 3000000000;
Will not compile, because 3,000,000,000 is greater than the maximum possible value of int, 2,147,483,647

5. Short form of declaration and initialization

Declaring a variable and assigning it a value can be written as a single statement. Most often that’s exactly what people do, since a variable is usually declared when there’s a need to store some value.

The statement looks like this:

int name = value;
Short form of creating and initializing a variable

Examples:

Command Note
int a = 5;
int b = 2*1000*1000*1000;
The variable will hold “2 billion”
int c = -10000000;
The variable will hold “minus 10 million”
int d = 3000000000;
Will not compile, because 3,000,000,000 is greater than the maximum possible value of int: 2,147,483,647

You can also declare several variables on one line. Then the statement looks like this:

int name1 = value1, name2 = value2, name3 = value3;
Short form of creating and initializing several variables

Examples:

Command Note
int a = 5, b = 10, c = a + b;
a equals 5, b equals 10, c equals 15

6. Evaluating integer expressions

On the right-hand side of the assignment operator (the equals sign) you can have any expression — a combination of numbers, variables, and the operators +, -, *, /.

You can also use parentheses ( ). In Java, as in mathematics, expressions inside parentheses are evaluated first and then the rest outside. Multiplication and division have equal precedence, and it’s higher than addition and subtraction.

Examples:

Command Note
int a = (2 + 2) * 2;
The variable will hold 8
int b = (6 - 3) / (9 - 6);
The variable will hold 1
int c = (-2) * (-3);
The variable will hold 6
int d = 3 / 0;
Executing this statement will cause an error, and the program will terminate.

Variables can also participate in an expression:

int a = 1;			// The variable a will hold 1
int b = 2;			// The variable b will hold 2
int c = a * b + 2;	// The variable c will hold 4

Moreover, the same variable can appear on both the left and right sides of the assignment operator:

int x = 5;		// The variable x will hold 5
x = x + 1;		// The variable x will hold 6
x = x + 1;		// The variable x will hold 7
x = x + 1;		// The variable x will hold 8
x = x + 1;		// The variable x will hold 9
x = x + 1;		// The variable x will hold 10

The point is that in Java the symbol = is not an equality sign but an instruction that stores the computed value of the expression on the right of = into the variable on the left of =.

7. Integer division

In Java, when dividing an integer by an integer, the result is always an integer. The remainder is discarded. In other words, the fractional part is thrown away.

Examples:

Command Division result Note
int a = 5 / 2;
2.5 The variable a will hold 2
int b = 20 / 3;
6.6667 The variable b will hold 6
int c = -6 / 5;
-1.2 The variable c will hold -1
int d = 1 / 2;
0.5 The variable d will hold 0

8. Remainder of integer division

In addition to addition, subtraction, multiplication, and division, Java also has the remainder operator. It uses the percent symbol — %. It gives the remainder when dividing one integer by another, not the fractional part.

Examples:

Command Division result Note
int a = 5 % 2;
2 with remainder 1 The variable a will hold 1
int b = 20 % 4;
5 with remainder 0 The variable b will hold 0
int c = 9 % 5;
1 with remainder 4 The variable c will hold 4
int d = 1 % 2;
0 with remainder 1 The variable d will hold 1

This is a very useful operator and it’s used quite often. For example, to determine whether a number is even, divide it by 2 and compare the remainder to zero. If the remainder is zero, the number is even; if it’s one, it’s odd.

This check looks like this:

(a % 2) == 0

Where, as you might have guessed, a % 2 gets the remainder when dividing by 2 (i.e., 0 or 1), and == is used to compare it to zero.

9. Increment and decrement

In programming you often need to increase or decrease a variable by one. Java has special operators for these actions:

The increment operator (increase by one) looks like this:

a++;
Increase by one

This statement does the same as a = a + 1; — it increases the variable a by one.

The decrement operator (decrease by one) looks like this:

a--;
Decrease by one

This statement does the same as a = a - 1; — it decreases the variable a by one.

Example 1

int x = 5;		// The variable x will hold 5
x++;			// The variable x will hold 6
x++;			// The variable x will hold 7
x++;			// The variable x will hold 8
x++;			// The variable x will hold 9
x++;			// The variable x will hold 10

Example 2

int x = 5;		// The variable x will hold 5
x--;			// The variable x will hold 4
x--;			// The variable x will hold 3
x--;			// The variable x will hold 2
x--;			// The variable x will hold 1
x--;			// The variable x will hold 0
x--;			// The variable x will hold -1
1
Task
JAVA 25 SELF, level 2, lesson 1
Locked
Main character's age 🦸‍♂️
Main character's age 🦸‍♂️
1
Task
JAVA 25 SELF, level 2, lesson 1
Locked
Dice Game Results 🎲
Dice Game Results 🎲
1
Task
JAVA 25 SELF, level 2, lesson 1
Locked
Distributing prizes among teams 🎁
Distributing prizes among teams 🎁
1
Task
JAVA 25 SELF, level 2, lesson 1
Locked
Changing the character's health in the game 📈
Changing the character's health in the game 📈
Comments (3)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Bidya Sharma Level 2, Ireland
21 July 2026
yeah why premium
John Q Citizen Level 3, Australia
4 September 2026
whaddya mean why? they gotta make some money somehow. They give just enough to get you interested for free. All the learn to coding sites do it.....
Landon Keith Level 4, United States of America, United States
3 May 2026
The endured shan't be swayed