Learn Kotlin in 12 mins
Saving a file
filename.kt
Printing Hello World
fun main() {
println("Hello World")
}
Variables
fun main(){
val firstName: String = "Rahul"
val is used for storing constants
var age: int = 16
var is used to store variables
}
Comments
// double slash could be used to write comments
Operators
val s1 = "Call me"
val s2 = "Maybe"
val combined = s1 + s2 ---> This will output "Call me Maybe"
val n1 = 20
val n2 = 30
val n3 = n1 + n2 | n1 - n2 | n1 * n2 | n1 / n2 | n1 % n2
Substituting value of variable in a String
-> println("The string $myString") --->Where myString is a variable having some value
Conditions
fun main() {
val examScore = 88
if(examScore > 70) {
println("You Passed")
}
Lists
-> Lists can be of two types: Mutable and immutable. By default the lists are immutable.
val names = listOf("Ali", "Maya", "Chen") // immutable list
val names = mutableListOf("Ali", "Maya", "Chen") // mutable list
names.add("Rose") // adding an element
For loops
->for(name in names) {
println(name)
}
->for(i in 1..5) {
println(i)
}
Functions
-> fun myFunction(){
println("Hello")
}
-> fun myfunction(name: String) {
println("Hello, $name") //this will print the name as well
}
fun main() {
myFun() //calling a function
}