Home »
Scala
Scala Char Data Type
By IncludeHelp Last updated : October 07, 2024
Char
Character (char) in Scala is a data type that is equivalent to 16-bit unsigned integer. The character data type stores a single character. It can be an alphabet, numbers, symbols, etc. The character takes 2 bytes while storing its literals.
When stored in memory, the character data type is stored as a Unicode number. This Unicode number is a unique unification number that is available for every character literal to be stored.
The use of char data type is not mandatory in scala. You can use var or val keyword in Scala to initialize them.
Syntax to declare a Char variable
//With data type
var variable_name : Char = 'I';
//Without data type
val variable_name = 'i';
Scala Example of Char Data Type
object MyClass {
def main(args: Array[String]) {
var ch = 'I';
println("The value of character ch is "+ch);
ch = 'H';
println("The changed value of character ch is " + ch);
}
}
Output
The value of character ch is I
The changed value of character ch is H
Code Logic
The above code is used to show initialization and operation on Scala char data type. The code initializes the value 'I' to a variable ch and then prints "The value of character ch is I". After that, it changes the value if ch from 'I' to 'H' and then again prints the changed value.