Kotlin – Basic Types

Kotlin – Basic Types

In this article, we will find out about the essential information types accessible in Kotlin programming dialect.

The portrayal of numbers in Kotlin is truly like Java, in any case, Kotlin does not permit interior change of various information types. Following table records diverse variable lengths for various numbers.

In the accompanying model, we will perceive how Kotlin works with various information types. Kindly enter the accompanying arrangement of code in our coding ground.

fun main(args: Array<String>) { 

    val a: Int = 77 
    val d: Double = 55.3
    val f: Float = 10.01f 
    val l: Long = 54000001
    val s: Short = 10 
    val b: Byte = 1 
    println("Your Int Value is "+a);      // out : 77
    println("Your Double  Value is "+d);  // out : 55.3
    println("Your Float Value is "+f);    // out : 10.01
    println("Your Long Value is "+l);     // out : 54000001
    println("Your Short Value is "+s);    // out : 10
    println("Your Byte Value is "+b);     // out : 1

  }

Characters

Kotlin speaks to character utilizing burn. Character ought to be proclaimed in a solitary statement like ‘c’. It would be ideal if you enter the accompanying code in our coding ground and perceive how Kotlin deciphers the character variable. Character variable can’t be proclaimed like number factors. Kotlin variable can be proclaimed in two different ways – one utilizing “var” and another utilizing “val”.

fun main(args: Array<String>) { 
    val letter: Char // defining a variable  
    letter = 'A'     // Assigning a value to it  
    println("$letter") 
 
} 

Boolean

Boolean is extremely basic like other programming dialects. We have just two qualities for Boolean – either evident or false. In the accompanying precedent, we will perceive how Kotlin deciphers Boolean

fun main(args: Array<String>) { 
    val letter: Boolean // defining a variable  
    letter = true    // Assinging a value to it  
    println("Your character value is "+"$letter") 
 
} 

Strings

Strings are character exhibits. Like Java, they are permanent in nature. We have two sorts of string accessible in Kotlin – one is called crude String and another is called circumvented String. In the accompanying model, we will make utilization of these strings.

fun main(args: Array<String>) { 
    var rawString :String ="I am Raw String!" 
    val escapedString : String ="I am escaped String!\n" 
    println("Hello!"+escapedString) 
    println("Hey!!"+rawString) 
     
} 

Arrays

Arrays are an accumulation of homogeneous information. Like Java, Kotlin bolsters varieties of various information types. In the accompanying model, we will make utilization of various arrays.

fun main(args: Array<String>) { 
    val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5) 
     
    println("Hey!! I am array Example"+numbers[2]) 
     
}

Collections

Gathering is an imperative piece of the information structure, which makes the product improvement simple for architects. Kotlin has two kinds of accumulation – one is unchanging gathering (which implies records, maps and sets that can’t be editable) and another is variable gathering (this sort of gathering is editable). It is imperative to remember the kind of gathering utilized in your application, as Kotlin framework does not speak to an explicit distinction in them.

fun main(args: Array<String>) { 
    val numbers: MutableList<Int> = mutableListOf(1, 2, 3) //mutable List 
    val readOnlyView: List<Int> = numbers                  // immutable list 
    println("my immutable list--"+numbers)        // prints "[1, 2, 3]" 
    numbers.add(4) 
    println("my immutable list after addition --"+numbers)        // prints "[1, 2, 3, 4]" 
    println(readOnlyView)     
    readOnlyView.clear()    // -> does not compile 
 
// gives error  
}

In gathering, Kotlin gives some helpful strategies, for example, first(), last(), channel(), and so forth. Every one of these techniques are self-engaging and simple to execute. Also, Kotlin pursues a similar structure, for example, Java while actualizing accumulation. You are allowed to actualize any accumulation of your decision, for example, Map and Set

In the accompanying model, we have executed Map and Set utilizing distinctive inherent strategies.

fun main(args: Array<String>) { 
    val items = listOf(1, 2, 3, 4) 
    println("First Element of our list----"+items.first()) 
    println("First Element of our list----"+items.last()) 
    println("Even Numbers of our List----"+items.filter { it % 2 == 0 })   // returns [2, 4] 
    val readWriteMap = hashMapOf("foo" to 1, "bar" to 2) 
    println(readWriteMap["foo"])  // prints "1" 
    val strings = hashSetOf("a", "b", "c", "c") 
    println("My Set Values are"+strings) 
}

Ranges

Extents is another novel normal for Kotlin. Like Haskell, it gives an administrator that causes you repeat through a range. Inside, it is actualized utilizing rangeTo() and its administrator frame is (..).

fun main(args: Array<String>) { 
    val i:Int =2 
    for (j in 1..4)  
    print(j) // prints "1234" 
    if (i in 1..10) { // equivalent of 1 <= i && i <= 10 
    println("we found your number --"+i) 
} 
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *