Home » Scala language

Char Data Type in Scala

Char in Scala: Char data type is a data type that accepts a character. In this Scala tutorial on data types, we will learn about character data types in Scala with working example code.
Submitted by Shivang Yadav, on July 16, 2019

Scala Char Data Type

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 define char variable in Scala:

    //With data type
    var variable_name : Char  = 'I';

    //Without data type
    val variable_name = 'i';

Example code to show Char data type in Scala

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);


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.


Top MCQs

Comments and Discussions!

© https://www.includehelp.com some rights reserved.