Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
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.
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 integerShort
: 16-bit signed integerInt
: 32-bit signed integerLong
: 64-bit signed integerFloat
: 32-bit floating-point numberDouble
: 64-bit floating-point numberEach 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()
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'
Kotlin's Boolean
type has two values, true
and false
. It supports standard logical operations:
val isTrue = true val isFalse = false
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)
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!"
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
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 }
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.
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
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
Working with strings in Kotlin: Kotlin provides powerful string manipulation features, including string templates.
val name = "Alice" val greeting = "Hello, $name!"
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)
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")
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
User-defined data types in Kotlin: You can define your own data types using classes.
data class Person(val name: String, val age: Int)
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) }
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"
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
Type conversion in Kotlin: You can convert between different types explicitly using conversion functions.
val stringValue: String = "42" val intValue: Int = stringValue.toInt()
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