The Char
type is used to represent letters (both uppercase and lowercase), digits, and other symbols. Each character is just a symbol enclosed in single quotes.
val lowerCaseLetter = 'a'
val upperCaseLetter = 'Q'
val number = '1'
val space = ' '
val dollar = '$'
This type can represent any symbol including hieroglyphs, as well as some special symbols.
A character can be also created using its hexadecimal code in the Unicode table. The code starts with
\u
.val ch = '\u0040' // it represents '@'
println(ch) // @
Although we use a sequence of characters to represent such code, the code itself represents exactly one character.
For example, Latin capital letters have hexadecimal codes from
'\u0041'
to '\u005A'
, and Latin small letters have codes from '\u0061'
to '\u007A'
.Retrieving subsequent characters
There are two operators for adding (
+
) and subtracting (-
) integer numbers in order to get the next and previous character according to the Unicode order.val ch1 = 'b'
val ch2 = ch1 + 1 // 'c'
val ch3 = ch2 - 2 // 'a'
It is possible to use increment (
++
) and decrement (--
) operators in prefix and postfix forms. The assignment operator combined with +
or -
also works, as well as +=
, -=
for characters.var ch = 'A'
ch += 10
println(ch) // 'K'
println(++ch) // 'L'
println(++ch) // 'M'
println(--ch) // 'L'
Note: It is not possible to multiply and divide characters by numbers.
Escape sequences
There are some special characters starting with backslash
\
, which are known as escape or control sequences. Most of them do not have corresponding symbols, and cannot be found on a keyboard. To represent such characters, we use a pair of regular symbols. In a program, this pair will be considered as exactly one character with the appropriate code. '\n'
is the newline character;'\t'
is the tab character;'\r'
is the carriage return character;'\\'
is the backslash character itself;'\''
is the single quote mark;'\"'
is the double quote mark.
Here are several examples:
print('\t') // makes a tab
print('a') // prints 'a'
print('\n') // goes to a new line
print('c') // prints 'c'
This code prints:
a
c
Note: There is also a character to represent a single space
' '
. It is just a regular character, not an escape sequence.Relational operations with characters
Characters can be compared using relational operations (
=
, <
, >
, <=
, >=
, !=
) according to their position in the Unicode table.println('a' < 'c') // true
println('x' >= 'z') // false
println('D' == 'D') // true
println('Q' != 'q') // true, because capital and small letters are not the same
println('A' < 'a') // true, because capital Latin letters are placed before small ones
Using relational operations and codes we can check whether a char is a digit or not. All ten digits have codes from
'\u0030'
to '\u0039'
.Here is a program to do that:
import java.util.*
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)
val ch = scanner.next().first()
val isDigit = ch in '\u0030'..'\u0039'
println(isDigit)
}
Note,
scanner
doesn't have a function to read characters. Instead, we read a string and takes its first character.If the input is a digit
'0'
, '1'
, '2'
, ..., '9'
(without quotes), the program prints true
. Otherwise, it prints false
.Processing characters
Each character has a set of useful functions. These functions can be used instead of working with codes.
isDigit()
returnstrue
if the given character represents a digit ('1'
,'2'
, etc); otherwise,false
;isLetter()
returnstrue
if the given character represents a letter ('a'
,'B'
,'m'
, etc); otherwise,false
;isLetterOrDigit()
returnstrue
if the given character represents a letter or a digit; otherwise,false
;isWhitespace()
returnstrue
if the given character represents a whitespace (' '
or'\t'
or'\n'
); otherwise,false
;isUpperCase()
returnstrue
if the given character is an uppercase character; otherwise,false
;isLowerCase()
returnstrue
if the given character is a lowercase character; otherwise,false
;toUpperCase()
returns the uppercase form of the given character;toLowerCase()
returns the lowercase form of the given character.
Let's look at some examples of the listed functions:
val one = '1'
val isDigit = one.isDigit() // true
val isLetter = one.isLetter() // false
val capital = 'A'
val small = 'e'
val isLetterOrDigit = capital.isLetterOrDigit() // true
val isUpperCase = capital.isUpperCase() // true
val isLowerCase = capital.isLowerCase() // false
val capitalE = small.toUpperCase() // 'E'
Naturally, there are a number of useful functions which we will learn throughout this course.