Data types in Kotlin

admin Avatar

Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM) and is interoperable with Java. In this blog post, we’ll explore the various data types available in Kotlin and provide examples for each one.

Boolean

The Boolean data type represents a logical value of either true or false. In Kotlin, Boolean values are represented using the Boolean type. Here’s an example:

var isTrue: Boolean = true

var isFalse: Boolean = false

Numeric

Kotlin supports several numeric data types, including Byte, Short, Int, Long, Float, and Double. Here are some examples:

var integer: Int = 42

var decimal: Double = 3.14

String

The String data type represents a sequence of characters. In Kotlin, Strings are represented using the String type. Here’s an example:

var message: String = “Hello, world!”

Arrays

Arrays are a collection of values of the same type. In Kotlin, arrays are represented using brackets and the type of the elements. Here’s an example:

var numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5)

Lists

Lists are similar to arrays, but their length is flexible. In Kotlin, lists are represented using the List interface. Here’s an example:

var letters: List<String> = listOf(“a”, “b”, “c”)

Maps

Maps are collections of key-value pairs. In Kotlin, maps are represented using the Map interface. Here’s an example:

var ages: Map<String, Int> = mapOf(“John” to 30, “Jane” to 25)

Sets

Sets are collections of unique values. In Kotlin, sets are represented using the Set interface. Here’s an example:

var uniqueNumbers: Set<Int> = setOf(1, 2, 3, 4, 5)

Advertisements

In conclusion, Kotlin provides a range of data types to work with, including Boolean, Numeric, String, Arrays, Lists, Maps, and Sets. By using these data types, developers can write clear and expressive code that is easy to read and maintain

Tagged in :

admin Avatar

More Articles & Posts