Other data types

5 0 0
                                        

Boolean - contains two values which true or false but if you include a null value, then you must put a suffix question mark in the data type to allow it to accept null values.

Note: When a data type accepts null values, boxing and unboxing of that variable is used.

Logical operators are used in Boolean like OR. This && and || used lazy to get its value. Lazy means it waits for the value before it continues the process.


Char - it is a character. It uses a single quote during initialization of value. It used escape sequence use like '

', '\t' and so on. You can use \u and hex value to get and display a character.

Strings - is an array of characters that is displayed using double quotes. You can use the concatenate (+) symbol to combine strings. You can use the escape characters like

and so on to format your strings.

===============

fun main(){

     println(getChars("Hello World"))

}

fun getChars(message: String): ArrayList<Char>{

     val array = arrayListOf<Char>()

     for(c in message){

          array.add(c)

     }

     return array

}

===============

Result:

[H, e, l, l, o,  , W, o, r, l, d]


Note: You can display raw strings using the delimited symbol (""")

===============

fun main(){

     println(displayRawString("Hello World"))

}

fun displayRawString(x: String): String{

     return """Sample of

     |raw string

     |from parameter $x

     """

}

===============

Result:

Sample of

raw string

from parameter Hello World

Note: The | above is just a margin prefix and nothing more. Escape characters is not allowed in raw strings. So, you can do it like this """{'$'}9.999""" if you want to add a dollar sign in a raw string.

Usually you might want to add a variable or an expression to a normal string. You can do so by just adding a dollar sign followed by the variable name or if it is an expression, just add a dollar sign followed by a brace and the expression within the brace.

================

fun main(){

     println(addTwoInt(5, 6))

}

fun addTwoInt(x: Int, y: Int): String{

     return "x: $x + y: $y = ${x+y}"

}

===============

Result:

x: 5 + y:6 = 11


Kotlin ProgrammingWhere stories live. Discover now