WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Basic Types


Kotlin Numbers

Kotlin does not allow internal conversion of different data types

There are following table lists different variable lengths for different numbers.

Type Size
Float 32
Double 64
Long 64
Int 32
Short 16
Byte 8

For example, we will see how Kotlin works with different data types.


fun main(args: Array) {
        val a: Int = 100000
        val d: Double = 1000.00
        val f: Float = 10.00f
        val l: Long = 10000004
        val s: Short = 10
        val b: Byte = 1
        println("Your Int Value is "+a);
        println("Your Double  Value is "+d);
        println("Your Float Value is "+f);
        println("Your Long Value is "+l);
        println("Your Short Value is "+s);
        println("Your Byte Value is "+b);
    }

it will generate the following output


    
Int Value is 100000
Double Value is 1000.00
Float Value is 10.00
Long Value is 10000004
Short Value is 10
Byte Value is 1

Kotlin Characters

Kotlin represents character using char. Character should be declared in a single quote like 'c'.

Kotlin variable can be declared in two ways - one using "var" and another using "val"


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


The above piece of code will yield the following output in the browser output window.

B

Boolean

Boolean is the expression – either true or false In the following example, we will see how Kotlin interprets Boolean.
fun main(args: Array<String>) {
   val letter: Boolean   // defining a variable 
   letter = false         // Assinging a value to it 
   println("Your character value is "+"$letter")
}

output Your character value is false

Strings

Strings are character arrays. Kotlin have two string : raw String and escaped String
fun main(args: Array) {
   var rawString :String  = "I am Raw String!"
   val escapedString : String  = "I am escaped String!\n"
   
   println("Hello!"+escapedString)
   println("Hey!!"+rawString)   
}
Output
Hello!I am escaped String!

Hey!!I am Raw String!
Arrays Arrays are a collection of homogeneous data.Kotlin supports arrays of different data types
fun main(args: Array<String>) {
   val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
   println("Hey!! I am array WELOOKUPS"+numbers[2])
}

Hey!! I am array WELOOKUPS3

Collections

Collection is a very important part of the data structure Kotlin has two types of collection : immutable collection and mutable collection immutable collection: lists, maps and sets that cannot be editable
mutable collection collection is editable.
fun main(args: Array<String>) { 
   val numbers: MutableList<Int> = mutableListOf(1, 2, 3) //mutable List 
   val readOnlyView: List<Int> = numbers                  // immutable list 
   println("my mutable list--"+numbers)        // prints "[1, 2, 3]" 
   numbers.add(4) 
   println("my mutable list after addition --"+numbers)        // prints "[1, 2, 3, 4]" 
   println(readOnlyView)     
   readOnlyView.clear()    // ⇒ does not compile  
// gives error  
}
mutable list of collection.

main.kt:9:18: error: unresolved reference: clear
   readOnlyView.clear()    // -> does not compile  
                 ^
kotline have method are first(), last(), filter(), etc we have implemented Map and Set using different built-in methods.
fun main(args: Array<String>) {
   val items = listOf(1, 2, 3, 4)
   println("First Element of our list----"+items.first())
   println("Last 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)
}


The above piece of code yields the following output in the browser.

First Element of our list----1
Last Element of our list----4
Even Numbers of our List----[2, 4]
1
My Set Values are[a, b, c]


Ranges

Ranges is another unique characteristic of Kotlin

rangeTo()

fun main(args: Array) {
   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)
   }
}
above piece of code yields the following output in the browser.
1234we found your number --2