Kotlin Data Types and Type Conversion
Expert-led Online Courses: Elevate Your Skills, Get ready for Future - Enroll Now!
Data types play a vital role in programming languages as they define the nature of data that can be stored and manipulated. Kotlin, a statically typed programming language, assigns a specific data type to each variable. This article explores the various data types available in Kotlin and discusses type conversion methods. Let’s start!!!
1. Kotlin Numeric Data Types:
Kotlin offers multiple numeric data types, such as integers, floating-point numbers, and characters.
For Example
val age: Int = 25 val pop_songs: Long = 80000000L val pi: Double = 3.14159 val weight: Float = 68.5f val grade: Char = 'M'
a. Int:
The ‘Int’ type represents signed integers, ranging from approximately -2 billion to 2 billion.
Example: `val age: Int = 25`
b. Long:
The ‘Long’ type stores large integers, with a wider range compared to ‘Int’.
Example: `val pop_songs: Long = 80000000L`
c. Double:
The ‘Double’ type handles floating-point numbers, supporting decimal places.
Example: `val pi: Double = 3.14159`
d. Float:
The ‘Float’ type manages floating-point numbers with lower precision than ‘Double’.
Example: `val weight: Float = 68.5f`
e. Char:
The ‘Char’ type represents a single character enclosed in single quotes.
Example: `val grade: Char = ‘M’`
2. Boolean Data Type in kotlin
The ‘Boolean’ type allows variables to have only two possible values: ‘true’ or ‘false’.
For Example
val isStudent: Boolean = true val hasPassedExam = false
3. Kotlin String Data Type:
The ‘String’ type stores a sequence of characters.
Example: `val name: String = “Mugdha Hazra”`
Type Conversion in Kotlin
Type conversion, or typecasting, refers to converting a value from one data type to another. Kotlin provides implicit and explicit type conversion methods.
1. Implicit Conversion in Kotlin:
Implicit conversion occurs automatically when assigning a value of one type to a variable of another compatible type.
For example
val intValue: Int = 10 val doubleValue: Double = intValue.toDouble()
In this implicit conversion example, an Int variable intValue is assigned the value 10. Then, the toDouble() function is called on intValue to convert it to a Double. The result is stored in the doubleValue variable.
2. Explicit Conversion in Kotlin:
Explicit conversion is necessary when converting a value of one type to another type that is not directly compatible. Kotlin offers various conversion functions for this purpose.
Here’s an example:
val doubleValue: Double = 3.14 val intValue: Int = doubleValue.toInt() val longValue: Long = 100L val floatValue: Float = longValue.toFloat() val charValue: Char = 'X' val intValue: Int = charValue.toInt()
In these explicit conversion examples, different numeric values are converted from one data type to another. doubleValue is explicitly converted from Double to Int using the toInt() function. Similarly, longValue is converted from Long to Float, and charValue is converted from Char to Int using the toInt() function.
In the above code snippets, you can observe the declaration and usage of different data types in various scenarios. Furthermore, type conversion is demonstrated both implicitly and explicitly.
Conclusion:
Understanding data types and type conversion is essential for effective programming in Kotlin. By utilizing appropriate data types and employing implicit or explicit type conversion when necessary, you can ensure that your code operates correctly and efficiently.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

