Character data type

The character data type is one of the data types available in Kotlin. This can contain 2 bytes of information and it can also store pretty much all the characters that you see on your keyboard. The syntax is the same as any other declaration—the word Char is used to declare a character variable and the value on the right-hand side must be enclosed by single quotes:

var mychar : Char = 'A'

Let's take a look at some examples. When character values are assigned to the Char data type, this can be displayed as follows:

var charA : Char = 'A'
var charZ : Char = 'Z'
var char1 = '1'
var char0 = '0'

Each character has a unique Unicode for its representation, and the character data type can be stored in Unicode values. Let's display A, Z, 1, and 0 by using a Unicode character. The syntax for storing the Unicode is pretty much the same, except u is required at the beginning of the code. This is demonstrated as follows:

var ucharA : Char = 'u0041'
var ucharZ : Char = 'u005A'
var uchar1 = 'u0031'
var uchar0 = 'u0030'

The following code shows how a character data type handles different characters: 

fun main(args: Array<String>) {
var charA : Char = 'A'
var charZ : Char = 'Z'
var char1 = '1'
var char0 = '0'
println("$charA $charZ $char1 $char0")

// Unicode Character
var ucharA : Char = 'u0041'
var ucharZ : Char = 'u005A'
var uchar1 = 'u0031'
var uchar0 = 'u0030'
println("$ucharA $ucharZ $uchar1 $uchar0")
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.226