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;
Where name is the variable name. Examples:
| Command | Description |
|---|---|
|
An integer variable x is created |
|
An integer variable count is created |
|
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;
Examples:
| Commands | Short form of the same commands |
|---|---|
|
|
|
|
|
|
4. Assigning values
To put a value into a variable of type int, use this statement:
name = value;
Where the value can be any integer expression. Examples:
| Command | Note |
|---|---|
|
|
|
The variable will hold “2 billion” |
|
The variable will hold “minus 10 million” |
|
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;
Examples:
| Command | Note |
|---|---|
|
|
|
The variable will hold “2 billion” |
|
The variable will hold “minus 10 million” |
|
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;
Examples:
| Command | Note |
|---|---|
|
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 |
|---|---|
|
The variable will hold 8 |
|
The variable will hold 1 |
|
The variable will hold 6 |
|
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 |
|---|---|---|
|
2.5 | The variable a will hold 2 |
|
6.6667 | The variable b will hold 6 |
|
-1.2 | The variable c will hold -1 |
|
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 |
|---|---|---|
|
2 with remainder 1 | The variable a will hold 1 |
|
5 with remainder 0 | The variable b will hold 0 |
|
1 with remainder 4 | The variable c will hold 4 |
|
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++;
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--;
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
GO TO FULL VERSION