Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin Data Types

Kotlin provides a rich set of built-in data types to represent numbers, characters, booleans, and more. Kotlin's type system is aimed at eliminating the danger of null references from code, one of the most common pitfalls in many programming languages.

1. Numbers:

Kotlin handles numbers in a way that's similar to Java, but with some additional features. Here are the primary numeric types:

  • Byte: 8-bit signed integer
  • Short: 16-bit signed integer
  • Int: 32-bit signed integer
  • Long: 64-bit signed integer
  • Float: 32-bit floating-point number
  • Double: 64-bit floating-point number

Each numeric type supports a set of basic arithmetic operators. Kotlin does not support implicit conversions between numeric types; you have to explicitly convert them:

val i: Int = 10
val l: Long = i.toLong()

2. Characters:

Character data type (Char) represents a single character. They are not numbers, so you can't directly treat them as one:

val c: Char = 'A'

3. Booleans:

Kotlin's Boolean type has two values, true and false. It supports standard logical operations:

val isTrue = true
val isFalse = false

4. Arrays:

Arrays in Kotlin are represented by the Array class. You can create an array using the arrayOf function:

val numbers = arrayOf(1, 2, 3, 4, 5)

Kotlin also provides specialized classes for primitive types to avoid boxing overhead: IntArray, FloatArray, CharArray, etc.

val intNumbers = intArrayOf(1, 2, 3, 4, 5)

5. Strings:

Strings are sequences of characters. In Kotlin, strings are immutable. They can be defined using double quotes:

val text: String = "Hello, Kotlin!"

String interpolation is also supported:

val name = "Alice"
val greeting = "Hello, $name!"

6. Nullable Types:

One of Kotlin's standout features is its built-in null safety. By default, variables cannot hold a null value:

var name: String = "Bob"
name = null  // Compilation error

To allow nulls, you can declare a variable as nullable using the ?:

var name: String? = "Bob"
name = null  // This is okay

When you declare a variable as nullable, you need to handle its nullability before using it:

val length: Int? = name?.length

7. Type Checking and Smart Casts:

You can check the type of a variable using the is keyword:

val obj: Any = "String"
if (obj is String) {
    print(obj.length)  // obj is automatically cast to String
}

Conclusion:

Kotlin's type system is expressive and safe. It aims to eliminate common mistakes and errors, such as null pointer exceptions, by making nullability explicit. Being familiar with these basic data types and their nuances is crucial when developing in Kotlin.

  1. Nullable types in Kotlin: Kotlin supports nullable types using the ? modifier, allowing variables to hold either a non-null value or null.

    var nullableString: String? = null
    
  2. Type inference in Kotlin: Kotlin has a powerful type inference system, allowing you to omit explicit type declarations when the compiler can infer them.

    val message = "Hello, Kotlin!"  // Type inferred as String
    
  3. Working with strings in Kotlin: Kotlin provides powerful string manipulation features, including string templates.

    val name = "Alice"
    val greeting = "Hello, $name!"
    
  4. Kotlin collections and their data types: Kotlin has a variety of collections, including lists, sets, maps, etc.

    val list: List<Int> = listOf(1, 2, 3)
    val set: Set<String> = setOf("apple", "banana")
    val map: Map<String, Int> = mapOf("one" to 1, "two" to 2)
    
  5. Arrays and lists in Kotlin: Arrays and lists are used to store collections of elements.

    val array: Array<Int> = arrayOf(1, 2, 3)
    val list: List<String> = listOf("apple", "banana")
    
  6. Type casting in Kotlin: Type casting is done using the as keyword or the safe cast operator as?.

    val value: Any = "Hello, Kotlin!"
    val stringValue: String = value as String
    
  7. User-defined data types in Kotlin: You can define your own data types using classes.

    data class Person(val name: String, val age: Int)
    
  8. Smart casts in Kotlin: Kotlin smart casts automatically cast a variable after checking its type.

    val value: Any = "Hello, Kotlin!"
    if (value is String) {
        // Automatically cast to String
        println(value.length)
    }
    
  9. Type aliases in Kotlin: Type aliases allow you to create custom names for existing types, improving code readability.

    typealias Name = String
    val userName: Name = "Alice"
    
  10. Primitive data types in Kotlin: Kotlin has special classes for primitive types, but they can be used seamlessly.

    val intValue: Int = 42
    val doubleValue: Double = 3.14
    
  11. Type conversion in Kotlin: You can convert between different types explicitly using conversion functions.

    val stringValue: String = "42"
    val intValue: Int = stringValue.toInt()
    
  12. Data type compatibility in Kotlin: Kotlin ensures type safety, preventing common pitfalls like null pointer exceptions and type-related errors.

    val name: String? = null
    val length: Int = name?.length ?: 0