← Back to Scala Course | Chapter 2: Variables & Types | Lesson 4 of 8

Char

A Char holds exactly one letter, digit, or symbol, like one tile from a word game. You write it in single quotes.

In this page:

  1. Char
Syntax
scala
val ch: Char = 'a'

Char

A Char represents a single UTF-16 character, written with single quotes, like A. Characters can be compared and cast to their underlying numeric code point with .toInt.

Double quotes are reserved for String, not Char, and mixing them up is a common beginner mistake.

Note: Casting a Char to Int with .toInt gives you its Unicode code point.

Example: Char

markup
object Main extends App {
  val letter: Char = 'A'
  val digit: Char = '7'

  println(letter)
  println(letter.toInt)
  println(digit.isDigit)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using double quotes for a Char, as in val c: Char = "A", which is a String and does not compile.
  2. Expecting letter.toInt to give the digit's value for 7, when it gives the character code 55.
  3. Trying to store two characters in a Char like AB, which is a compile error.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.