CodeGym /Courses /Kotlin SELF /Integers: the Int type, operations with Int

Integers: the Int type, operations with Int

Kotlin 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 (“whole” in English), which kind of hints that this type lets you store whole numbers.

Variables of type Int can store integers in the range 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 values are related to how computer memory is organized. In Kotlin (on the JVM), 4 bytes of memory are allocated for the Int type. Each byte of memory consists of 8 bits. Each bit can take only 2 values — 0 or 1. An Int variable contains 32 bits and can take 4,294,967,296 values.

Half of this range is used for negative numbers, and the other half for positive numbers. That’s how we get the range 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 a variable in code that can store integers, use a statement of the form:

var name: Int
Declaring a variable of type Int

Where name is the variable name. Examples:

Statement Description
var x: Int
Creates an integer variable x
var count: Int
Creates an integer variable count
var currentYear: Int
Creates an integer variable currentYear

The size (case) of letters matters: the statements var color: Int and var Color: Int will declare two different variables.

But the statements Int Color and INT COLOR won’t be understood by the compiler at all and will result in an error. In Kotlin, you write var/val first, then the variable name, then the type after a colon :. The type is called Int and is written exactly like that (with a capital I).

3. Short form for creating variables

If you need to create many variables of the same type in one place in a program, in Java you can do it using a shortened comma-separated form.

int x, y, z;

In Kotlin, this form does not exist: each variable is declared separately. If you want, you can write several declarations on one line by separating them with ; (a semicolon), but more often they are written on separate lines.

Examples:

var x: Int; var y: Int; var z: Int

or

var x: Int; 
var y: Int; 
var z: Int

4. Assigning values

To put a value into a variable of type Int, use the statement:

name = value
Assigning a value to a variable

Where the value can be any integer expression. Examples:

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

5. Initializing variables

Creating (declaring) a variable and assigning it a value can be written as a single statement. Most often this is exactly what is done, since a variable is usually declared when there is a need to store some value.

In Kotlin it looks like this:

var name: Int = value
Short form for creating and initializing a variable

Examples:

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

6. Evaluating integer expressions

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

You can also use parentheses ( ). In Kotlin, as in mathematics, expressions inside parentheses are evaluated first, and then the outer ones. Multiplication and division have the same precedence, and it is higher than addition and subtraction.

Examples:

Statement Note
var a: Int = (2 + 2) * 2
The variable will contain the value 8
var b: Int = (6 - 3) / (9 - 6)
The variable will contain the value 1
var c: Int = (-2) * (-3)
The variable will contain the value 6
var d: Int = 3 / 0
When this statement is executed, an error will occur and the program will terminate.

Variables can also participate in an expression:

var a: Int = 1            // The variable a will contain the value 1
var b: Int = 2            // The variable b will contain the value 2
var c: Int = a * b + 2    // The variable c will contain the value 4

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

var x: Int = 5        // The variable x will contain the value 5
x = x + 1             // The variable x will contain the value 6
x = x + 1             // The variable x will contain the value 7
x = x + 1             // The variable x will contain the value 8
x = x + 1             // The variable x will contain the value 9
x = x + 1             // The variable x will contain the value 10

The point is that in the Kotlin language, the symbol = is not a sign of equality, but a command that stores, in the variable to the left of =, the evaluated value of the expression to the right of =.

7. Dividing integers

In Kotlin, when you divide an integer by an integer, the result is always an integer. The remainder is discarded. In other words, the fractional part is discarded.

Examples:

Statement Division result Note
var a: Int = 5 / 2
2.5 The variable a will contain the value 2
var b: Int = 20 / 3
6.6667 The variable b will contain the value 6
var c: Int = -6 / 5
-1.2 The variable c will contain the value -1
var d: Int = 1 / 2
0.5 The variable d will contain the value 0

8. Remainder of integer division

In addition to addition, subtraction, multiplication, and division, Kotlin also has the “remainder” operator for integers. It uses the percent symbol — %. This is the remainder of dividing an integer by an integer, not the fractional part.

Examples:

Statement Division result Note
var a: Int = 5 % 2
2 with remainder 1 The variable a will contain the value 1
var b: Int = 20 % 4
5 with remainder 0 The variable b will contain the value 0
var c: Int = 9 % 5
1 with remainder 4 The variable c will contain the value 4
var d: Int = 1 % 2
0 with remainder 1 The variable d will contain the value 1

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

This check looks like this:

(a % 2) == 0

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

9. Increment and decrement

In programming, you very often need to increase or decrease a variable by one. For these actions, Kotlin has special operators:

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

a++
Increase by one

This operator does the same thing 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 operator does the same thing as a = a - 1 — it decreases the variable a by one.

Example 1

var x: Int = 5        // The variable x will contain the value 5
x++                   // The variable x will contain the value 6
x++                   // The variable x will contain the value 7
x++                   // The variable x will contain the value 8
x++                   // The variable x will contain the value 9
x++                   // The variable x will contain the value 10

Example 2

var x: Int = 5        // The variable x will contain the value 5
x--                   // The variable x will contain the value 4
x--                   // The variable x will contain the value 3
x--                   // The variable x will contain the value 2
x--                   // The variable x will contain the value 1
x--                   // The variable x will contain the value 0
x--                   // The variable x will contain the value -1
1
Task
Kotlin SELF, level 2, lesson 1
Locked
Kiosk checkout
Kiosk checkout
1
Task
Kotlin SELF, level 2, lesson 1
Locked
Math on the scoreboard
Math on the scoreboard
1
Task
Kotlin SELF, level 2, lesson 1
Locked
Quest Timer
Quest Timer
1
Task
Kotlin SELF, level 2, lesson 1
Locked
Warehouse packing
Warehouse packing
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION