BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Type Casting with examples

Last Updated: February 20, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

Type casting is a process of converting one data type to another type, for example – converting int to long, long to double etc. In this tutorial we will learn how to do type conversion in Kotlin.

Type conversion in Kotlin vs Java

In java, one type is automatically converted to other type (in some cases), In Kotlin we need to explicitly convert the type.

For example:
Java:
Int is automatically converted to Long data type as long is larger than int.

// This code is valid in Java, even though we are converting int to long
// because long is larger than int and it can hold integers
int num1 = 101;
long num2 = num1; 

Kotlin:
In Kotlin the conversion is not automatic, we need to explicitly do the type conversion.

// This code is invalid in Kotlin. This will cause compile time 
// error, type mismatch
val num1: Int = 101
val num2: Long = num1

Correct code in Kotlin:
We use the toLong() function to convert int to long in Kotlin.

val num1: Int = 101
val num2: Long = num1.toLong()

More functions for type conversion in Kotlin

In the above example, we have seen how we used toLong() function to convert an integer to Long type. Similarly we have other functions available in Kotlin to help us in type conversion.

1. toChar() – To convert a type to Char type.
2. toInt() – To convert a type to Int type.
3. toLong() – To convert a type to Long type.
4. toFloat() – To convert a type to Float type.
5. toDouble() – To convert a type to Double type.
6. toByte() – To convert a type to Byte type.
7. toShort() – To convert a type to Short type.

Simple Type Casting Examples

/**
 * created by Chaitanya for Beginnersbook.com
 */
package beginnersbook

fun main(args : Array<String>){

    /**
     * Double to int type casting
     */
    println("4.554 to int: " + (4.554.toInt()))

    /**
     * int to Char type casting
     */
    println("66 to Char: " + (65.toChar()))

    /**
     * Char to int type casting
     */
    println("B to int: " + ('B'.toInt()))
}

Output:
Kotlin Type Casting

What happens when we convert a larger type to smaller type?

When converting a larger type to a smaller type, we have two possible outcomes. If the value that we are converting to a smaller type is outside the range of target type , the result is then truncated. However when the value is within the range of target type, it is converted without being truncated.

Example 1: When the value is outside the range of target type

In this example we are converting a long type to int type where the value of long type is outside the range of integer type.

fun main(args : Array<String>) {
    val num1: Long = 5453448989999998988
    val num2: Int = num1.toInt()
    println("Number num1 is: $num1")
    println("Number num2 is: $num2")
}

Output:

Number num1 is: 5453448989999998988
Number num2 is: 2041161740

Example 2: When the value is within the range of target type

In this example we are converting a long type to int type where the value of long type is within the range of integer type.

fun main(args : Array<String>) {
    val num1: Long = 5453448
    val num2: Int = num1.toInt()
    println("Number num1 is: $num1")
    println("Number num2 is: $num2")
}

Output:

Number num1 is: 5453448
Number num2 is: 5453448
❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Keywords, Soft Keywords and Identifiers
  2. Kotlin Nested and Inner Class with examples
  3. Kotlin Class and Objects – Object Oriented Programming (OOP)
  4. Kotlin Array
  5. Kotlin Constructors with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap