Learn
Using Number Variables
Number Types

We’ve briefly worked with integers in the previous lesson, and in this section, we’ll take a deeper dive into the various types of numbers that exist in Kotlin.

Kotlin numbers fall into two categories: integers and decimals as part of the Number superclass. Take a look at the image on the right. Each book occupies a certain amount of space on the shelf depending on its size. The same goes for the various numerical data types in Kotlin. The larger a variable is in size (Bits), the more space it occupies in our computer’s memory.

The books on the left side represent 4 different types of data for an integer value:

Name Bit (Size) Min Value Max Value
Long 64 -9,223,372,036,854… 9,223,372,036,854…
Int 32 -2,147,483,648 2,147,483,647
Short 16 -32,768 32,767
Byte 8 -128 127

A Long variable is meant to store significantly large values, whereas a Byte variable cannot store a value larger than 127. Hence, their size and occupation in memory vary.

The books on the right represent 2 various types of data for decimal or floating-point values:

Name Bit (Size) Decimal Digit Precision
Double 64 15-16
Float 32 6-7

A Double variable contains a longer, more precise decimal precision than a Float. For example:

var doubleNum: Double = 6.0 / 13.0 var floatNum: Float = 6.0f / 13.0f println(doubleNum) // Prints: 0.46153846153846156 println(floatNum) // Prints: 0.46153846

When declaring a numerical variable in Kotlin, the compiler will determine which of the above data types to assign to it. The default type for whole numbers is Int and the default type for decimals is Double, thus we’ll be working with these two types of data more closely throughout this lesson.

Instructions

Click Up Next when you’re ready to move on.

Folder Icon

Take this course for free

Already have an account?