Char type
- Characters are represented by the Char keyword.
- They cannot be treated directly as numbers but you can perform explicit conversion functions according to your needs. Let’s understand this by example.
char a = 65 // Compile time error.
This code works fine in Java but will throw an error in kotlin.
Character literals go in single quotes like ‘V’, ‘J’, etc.
In kotlin, strings are represented by the String class. String literals go inside the double quotes like “Viveki”, “Jay”, etc.
Special characters can be escaped using a backslash.
The following escape sequences are supported:
- \t — To print tab
- \b — To use a backslash
- \n — newline character
- \r — carriage return
- \’ — To print single quote
- \” — To print a double quote
- \ — To print a backslash
- \$ — To print the dollar sign
- To encode any other character like hexadecimal or binary use Unicode escape sequence syntax: ‘\uFF004’.
Boolean Type
- Boolean values are represented by Boolean keyword.
- Boolean has two values either true or false. Both must be in lower case and cannot represent as 0 and 1.
So, guys, that’s it for char and boolean in kotlin. Feel free to let me know if I missed something.
Till then Keep Coding, Keep Loving. Catch you up in another article.
Top comments (0)