Kotlin
Kotlin
Numbers:
Nullable types: All types in Kotlin can be nullable by appending ? to the type name
(e.g., String?, Int?)
fun main() {
var name = "Elwin"
name = "Jason"
print(name)
print("Hello $name")
}
variable is a container that holds a certain value at certain point of time
var = Mutable
val = Immutable
---------------------------------------------------------------------------
EXPLICIT INITIALIZATION:
The below is an explicit initialization
fun main() {
---------------------------------------------------------------------------
Double has more precision so to build a application that requires more precise
values use double
---------------------------------------------------------------------------
LONG TYPE:
fun main() {
val onelong = 1L
KOTLIN OPERATIONS:
fun main() {
val a = 23
val b = 12
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b)
}
val a = 23
val b = 12
print(a/b)
Output : 1
val a = 23.0
val b = 12
print(a/b)
Output : 1.9166666666666667
---------------------------------------------------------------------------
fun main() {
val amount = 900
if (amount >= 1000) {
print("You are wealthy")
} else {
print("You are getting by")
}
}
If we want to create a more branched tree we can use the when expression.
fun main() {
val amount = 999
when(amount) {
100 -> print("You have 100")
125 -> print("You are getting there....")
999 -> print("You are really close")
else -> {
print("$amount is not going to work")
}
}
fun main() {
val amount = 99
when(amount) {
in 1..100 -> print("The amount is between 1 and 100")
!in 10..90 -> print("The amount is outside the range")
}
}
Input : amount = 99
Output: The amount is between 1 and 100
Input : amount = 0
Output : The amount is outside the range
---------------------------------------------------------------------------
FOR LOOPS:
fun main() {
for (i in 1..3) {
println(i)
}
}
Output :
1
2
3
fun main()
for (i in 1..10) {
if (i%3 == 0) println(i) else println("--")
}
}
Output:
--
--
3
--
--
6
--
--
9
--
---------------------------------------------------------------------------
FUNCTIONS:
Keyword - fun
fun main()
{
hello()
}
fun hello() {
print("This is a function")
}
fun main() {
ran(1,100) - These are called arguments.
Arguments are values that are declared within a function when
a function is called.
}
fun main() {
calculate(1,100,"is the multiple of",3)
}
fun calculate(first:Int,second:Int,message:String,mof:Int){
for(i in first..second){
if(i%mof == 0){
println("$i $message $mof")
}
}
}
DEFAULT ARGUMENTS:
------- ---------
fun main() {
calculate(message = "is the multiple of",mof = 11)
}
fun main() {
calculate(first = 20,
second = 200,
message = "is the multiple of",
mof = 11)
}
RETURNING INT:
--------- ---
fun main() {
// val catage= calculateCatAge(age = 12)
// print("This cat is $catAge years old")
print("This cat is "+calculatecatAge(age = 11)+" years old")
}
// fun calculatecatAge(age:Int): Int{
// return age * 7
// }
fun calculatecatAge(age : Int): Int = age * 7
RETURNING BOOLEAN:
--------- -------
fun main() {
val age = calculatecatAge(age = 10)
if(checkage(age)){
print("This cat is old ($age years old)")
}
else{
print("This cat is young ($age years old)")
}
}
fun calculatecatAge(age : Int): Int = age * 7
LAMBDA FUNCTION:
------ --------
fun main() {
println(sum(1,2))
println(age(6))
}
val sum : (Int,Int)->Int = {a , b -> a + b}
// val lambdaname : Type = {paramaterlist -> codeBody}
fun main() {
name("Slim Shady")
lname("Eminem")
}
val name: (String) -> Unit = {println("My name is, My name is, My name is Chica
Chica Chica Chica $it")}
FUNCTION AS PARAMTER:
-------- -- --------
fun main() {
message("Hello there,"){
print(it)
add(12,12)
}
}
COLLECTION:
-----------
Kotlin Collections Functions Cheat Sheet - Article To Refer for More
fun main() {
val mutablelist = mutableListOf("Rocky" , "Blacky" , "Tinku" , "Jimmy")
mutablelist.add(1,"Sammy")
mutablelist.remove("Jimmy")
mutablelist.removeAt(1)
mutablelist.forEach{
println(it)
}
SETS IN KOTLIN:
---- -- -------
fun main() {
val myset = setOf("A","AB","C","B","C")
val mutableset = mutableSetOf(1,2,3,4,5,6,7,8)
println(myset)
println(mutableset)
myset.forEach{
println(it)
}
mutableset.add(9)
mutableset.add(2)
mutableset.forEach{
println(it)
}
println(mutableset)
MAPS IN KOTLIN:
---- -- -------
fun main() {
val secmap = mapOf("Up" to 1,
"Down" to 2,
"Left" to 3,
"Right" to 4)
println(secmap)
println(secmap.keys)
println(secmap.values)
if("Up" in secmap) println("Yo nigga")
print("\n\n")
INITIALIZING LISTS:
------------ ------
Code:
-----
fun main() {
val list = mutableListOf<String>()
list.add("hola")
println(list)
list.add("Mi Amigos")
println(list)
for(i in 1..10) list.add(i,"Hey $i")
println(list)
}
<String> - This specifies that only string items can be added into the list
Output:
-------
[hola]
[hola, Mi Amigos]
[hola, Hey 1, Hey 2, Hey 3, Hey 4, Hey 5, Hey 6, Hey 7, Hey 8, Hey 9, Hey 10, Mi
Amigos]
EMPTY COLLECTIONS:
----- ------------
Code:
-----
fun main() {
val emptylist = emptyList<String>()
val emptyset = emptySet<String>()
val emptymap = emptyMap<String,Int>()
println(emptylist)
println(emptyset)
println(emptymap)
}
Output:
-------
[]
[]
{}
FILTER METHOD:
------ -------
Code:
-----
fun main() {
Output:
-------
[Roberto]
[]
[James, Roberto, Mourino]
[Roberto, Mourino]
[James, Jose]
[]
[James, Jose]
[James]