🎓 A1Learner
🔍
0 of 50 lessons completed

Scala Data Types

A data type is the kind of a value, and it decides what a program can do with that value. Scala values include whole numbers, decimal numbers, text, single characters, and true or false values.

Int division drops the decimal part

Dividing two whole numbers gives a whole number result. Adding a decimal part to one number gives a decimal result.

@main def main(): Unit =
  println(7 / 2)
  println(7.0 / 2)

String holds text and Char holds one character

The String value uses double quotes around several letters. The Char value uses single quotes around one letter.

@main def main(): Unit =
  val word: String = "Scala"
  val letter: Char = 'S'
  println(word)
  println(letter)

Boolean values are true or false

A Boolean holds exactly one of the two values. Both values print as their own words.

@main def main(): Unit =
  val open: Boolean = true
  val closed: Boolean = false
  println(open)
  println(closed)

Example

@main def main(): Unit =
  val count: Int = 42
  val price: Double = 3.5
  val title: String = "Course"
  val letter: Char = 'C'
  val open: Boolean = true
  println(count)
  println(price)
  println(title)
  println(letter)
  println(open)

Key points

  • Int: Int holds whole numbers like 7 and -3.
  • Long: Long holds whole numbers too, and it can hold much larger ones than Int.
  • Double: Double holds numbers with a decimal part like 3.5.
  • String: String holds text inside double quotes.
  • Char: Char holds one character inside single quotes.
  • Boolean: Boolean holds one of two values, true or false.

In this example, five values are stored with five different types. Each println prints one value, so the lines show an Int, a Double, a String, a Char, and a Boolean in order.

Note

dividing two Int values in Scala drops the decimal part, so 7 / 2 gives 3.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Scastie

Exercise

What is the result of 7 / 2 when both numbers are Int values in Scala?