Kotlin for Swift Developers and vice versa — part I
Swift and Kotlin Variable Types
--
I am an iOS engineer and Swift is my language of choice but lately I started learning Kotlin and while learning I found out the similarities between both the languages. So, I thought of capturing the similarities to help other engineers out there who are planning to learn Kotlin as Swift developers or vice versa.
In this blog post you will read about Swift and Kotlin variable types.
Variables/Mutable types
A variable is a way of giving a name to a piece of data so that we can reference it later in program execution.
Declaring a variable in swift:
var name = "My name in Swift"
Swift infers the data type and hence you don't need to explicitly mention the datatype like this:
var name: String = "My name in Swift"
When compared to swift, there is NO CHANGE when we declare a variable in Kotlin.
Declaring a variable in Kotlin:
var name = "My name in Kotlin"
Like Swift, Kotlin also infers the datatype implicitly, so there is no need to explicitly mention the datatype.
Constants/Immutable types
A constant is also way of giving a name to a piece of data so that we can reference it later in program execution but constants are immutable types and can’t be changed later in program execution.
To declare a constant in Swift, we use let as the keyword:
let name = "My name in Swift"
Swift infers the data type and hence you don’t need to explicitly mention the datatype like this:
let name: String = "My name in Swift"
When compared to swift, there is a CHANGE when we declare a constant in Kotlin.
To declare a constant/immutable type in Kotlin, we use val as the keyword:
val name = "My name in Kotlin"
Like Swift, Kotlin also infers the datatype implicitly, so there is no need to explicitly mention…