Зміст курсу
Introduction to Scala
Introduction to Scala
More about Char
In Scala, the Char
data type represents a single 16-bit Unicode character. It's a value type that is used to store characters (e.g. letters and punctuation marks).
Initially, we mentioned that Char
in Scala has a range of values from 0 to 2^16 - 1 (65535). This range might seem peculiar at first, as we typically associate characters with letters and symbols, not numbers. To understand this, let's dive into the concept of character encoding.
Character Encoding
Let's first discuss what character encoding actually stands for.
Scala uses Unicode, a universal character encoding standard, which provides a unique number for every character, irrespective of the platform, program, or language.
The Char
data type in Scala uses the UTF-16 encoding, where each character is represented by one or two 16-bit code units. This is why Scala Char is 2 bytes (16 bits), allowing a vast range of characters to be represented.
The first 128 characters with their ecnodings can be found in the ASCII table. They include English letters, numbers and special characters.
Mathematical Operations with Char
Due to character encoding we are able to perform all the mathematical operations with the Char
data type. Let's take a look at an example:
Main
object Main { def main(args: Array[String]): Unit = { val letter = 'A' println(letter.toInt) // Results in 65 val nextLetterEncoding = letter + 1 // Results in 66 println(nextLetterEncoding) val nextChar = nextLetterEncoding.toChar // Results in 'B' println(nextChar) } }
As you can see, we have used here the toInt
and toChar
type casting methods to convert Char
to Int
and vice versa.
You can find all the details of this code explained by clicking the button below:
Let's now take a look at another example where we add two variables of Char
data type:
Main
object Main { def main(args: Array[String]): Unit = { val char1 = '(' // The encoding is 40 val char2 = 'A' // The encoding is 65 val char3 = (char1 + char2).toChar // Results in i println(char3) } }
Once again, we can refer to the ASCII table to find that the encding for (
is 40 and the encoding for A us 65
.
Here, we converted the result (105) to Char
which corresponds to the letter i
.
Дякуємо за ваш відгук!