EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Kotlin Basic Concepts

In Kotlin, data types are the foundation of variable storage. Each variable must be assigned a specific data type, which determines the kind of data it can hold and the operations that can be performed on it.

1. Int (Integer): Represents a 32-bit signed integer. Suitable for whole numbers.

These data types contain integer values.

Kotlin Basic Concepts
Data TypeBitsMin ValueMax Value
byte8 bits-128127
short16 bits-3276832767
int32 bits-21474836482147483647
long64 bits-9223372036854775808 9223372036854775807

Example:

fun main(args: Array<String>) {
    var myInt = 35
    var myLong = 23L // suffix L for long integer

    println("My integer: $myInt")
    println("My long integer: $myLong")

    println("Smallest byte value: ${Byte.MIN_VALUE}")
    println("Largest byte value: ${Byte.MAX_VALUE}")

    println("Smallest short value: ${Short.MIN_VALUE}")
    println("Largest short value: ${Short.MAX_VALUE}")

    println("Smallest integer value: ${Int.MIN_VALUE}")
    println("Largest integer value: ${Int.MAX_VALUE}")

    println("Smallest long integer value: ${Long.MIN_VALUE}")
    println("Largest long integer value: ${Long.MAX_VALUE}")
}

Output:

My integer: 42
My long integer: 123456789

Smallest byte value: -128
Largest byte value: 127

Smallest short value: -32768
Largest short value: 32767

Smallest integer value: -2147483648
Largest integer value: 2147483647

Smallest long integer value: -9223372036854775808
Largest long integer value: 9223372036854775807

Smallest float value: 1.4E-45
Largest float value: 3.4028235E38

Smallest double value: 4.9E-324
Largest double value: 1.7976931348623157E308

2. Boolean Data Type : The Boolean data type represents one bit of information, with two possible values: true or false.

Kotlin Basic Concepts
Data TypeBitsValue Range
Boolean1true, false

Example Program:

fun main(args: Array<String>) {
    if (true is Boolean) {
        println("Yes, true is a boolean value")
    }
}

Output:

Yes, true is a boolean value

3. Character Data Type : Represents characters such as letters, digits, and symbols.

Kotlin Basic Concepts
Data TypeBitsMin ValueMax Value
Char16‘\u0000’‘\uFFFF’

Example Program:

fun main(args: Array<String>) {
    var alphabet: Char = 'C'
    println("C is a character: ${alphabet is Char}")
}

Output:

C is a character: true
End of lesson.