Data types in kotlin is invariant. Meaning it never change.
==============
var x: Double
x = 1
==============
Result:
Error
Note: It is an error because you change it. Data types never change. You declare a double, then you assign a double. 1 is an integer. So, make it 1.0 to make the value a double.
Arrays stores a number of datum in a number of elements of the same data type. Array is invariant too when it comes to the type parameter it uses. You can create an array using the following:
1. arrayOf()
     This is a method where you pass a varargs of value into it separated by a comma.
     val array = arrayOf(1, 2, 3)
2. arrayOfNulls()
     This is a method where you pass in the size of the array. Each element of this array contains null.
     val array = arrayOfNulls<Int>(5)
3. Array(size){lambda function}
    This is a constructor of the array where it takes in a two parameters. First is an Int whose value determines the size of the array. Second is a function, a lambda function. Lambda function is used in a standard functional interface. An interface with one abstract method that needs to be implemented. Note the second parameter should be within the parenthesis but since it is a lambda, you can just put it a body. Lambda will be discuss in the lambda section.
================
function main(){
    usingArrayConstruct(5)
}
fun usingArrayConstruct(x: Int){
     val array: Array<Int> = Array(x){i -> i * 10}
     for(elem in array.iterator()){
          print("$elem ")
     }
}
================
Result:
0 10 20 30 40
Note: I access the elements through iterator but you can always use the [] notation to get or set the value in an array like array[0] = 100.
4. primitive type arrays like ByteArray, IntArray and so on.
 IntArray for example works the same way as above.
     a. intArrayOf()
          intArrayOf(1, 2, 3) => This has a value of (1, 2, 3)
    b. IntArray(size: Int)
          IntArray(5) => This has a value of (0, 0, 0, 0, 0)
    c. IntArray(size: Int){//value}
          IntArray(3){101} => This has a value of (101, 101, 101)
     d. IntArray(size: Int){//lambda function}
          IntArray(4){i - > i * 2} => This has a value of (0, 2, 4, 6)
                                      
                                          
                                   
                                              YOU ARE READING
Kotlin Programming
RandomI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
 
                                               
                                                  