Kotlin Type Conversion

Introduction

Type conversion is the process of converting a value from one data type to another. In Kotlin, type conversion is necessary when you want to perform operations on values of different types. Unlike some programming languages, Kotlin does not perform implicit type conversions. Instead, you must explicitly convert types using built-in functions. This chapter will cover how to convert between different types in Kotlin.

Explicit Type Conversion

Kotlin provides several functions to explicitly convert values from one type to another. Here are the common conversion functions:

Conversion Functions

  • toByte(): Converts to Byte.
  • toShort(): Converts to Short.
  • toInt(): Converts to Int.
  • toLong(): Converts to Long.
  • toFloat(): Converts to Float.
  • toDouble(): Converts to Double.
  • toChar(): Converts to Char.
  • toString(): Converts to String.

Examples of Type Conversion

1. Converting Numeric Types

Example:

fun main() {
    val number: Int = 42
    val byteNumber: Byte = number.toByte()
    val shortNumber: Short = number.toShort()
    val longNumber: Long = number.toLong()
    val floatNumber: Float = number.toFloat()
    val doubleNumber: Double = number.toDouble()

    println(byteNumber)  // Output: 42
    println(shortNumber)  // Output: 42
    println(longNumber)  // Output: 42
    println(floatNumber)  // Output: 42.0
    println(doubleNumber)  // Output: 42.0
}

2. Converting to and from Strings

Example:

fun main() {
    val number: Int = 123
    val numberString: String = number.toString()
    println(numberString)  // Output: "123"

    val stringNumber: String = "456"
    val intNumber: Int = stringNumber.toInt()
    val doubleNumber: Double = stringNumber.toDouble()

    println(intNumber)  // Output: 456
    println(doubleNumber)  // Output: 456.0
}

3. Converting Characters to Numeric Types

Example:

fun main() {
    val char: Char = 'A'
    val charCode: Int = char.code
    println(charCode)  // Output: 65 (ASCII code of 'A')

    val number: Int = 66
    val charFromNumber: Char = number.toChar()
    println(charFromNumber)  // Output: 'B'
}

4. Converting Boolean to String

Example:

fun main() {
    val isKotlinFun: Boolean = true
    val booleanString: String = isKotlinFun.toString()
    println(booleanString)  // Output: "true"

    val stringBoolean: String = "false"
    val booleanValue: Boolean = stringBoolean.toBoolean()
    println(booleanValue)  // Output: false
}

Handling Invalid Conversions

When converting a String to a numeric type, it’s essential to ensure that the String is a valid representation of the numeric type to avoid runtime exceptions.

Example:

fun main() {
    val invalidNumberString: String = "abc"
    val number: Int? = invalidNumberString.toIntOrNull()
    if (number != null) {
        println(number)
    } else {
        println("Invalid number format")
    }
}

Conclusion

In this chapter, you learned about type conversion in Kotlin. You explored how to explicitly convert values between different data types using built-in functions and how to handle invalid conversions gracefully. Understanding type conversion is crucial for performing operations on different data types and ensuring the accuracy and reliability of your Kotlin programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top