Start learning free

LESSON 7 OF 414 · WELCOME TO SCALA

Strings vs Numbers in Scala: Int and String Literals

Lesson Read the full explanation

  • Scala distinguishes between numbers and strings!


  • In Scala, "15" (with quotes) is considered a String, while 15 (without quotes) is considered a number (an Int, for example).


  • For example, computers can perform mathematical operations on numbers, but not on strings. More on this in the next lessons.

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · WELCOME TO SCALA

    Strings Vs Numbers in Scala, explained

    Quotes make the difference

    In Scala, the same digits can be a number or text depending on the quotes around them. 15 is an Int, a whole number. "15", with double quotes, is a String: two characters that happen to be digits. They look alike but behave differently.

    val count = 15 + 1       // 16, arithmetic on numbers
    val text = "15" + 1      // "151", text joined with the digit 1
    

    Adding two numbers gives their sum, 16. Adding a string and a number builds a longer string, "151", because + on a string means concatenation.

    Types you write and types Scala infers

    You don't always have to write the type. In val n = 15 Scala sees an integer literal and decides n is an Int. In val s = "15" it sees a string literal and decides s is a String. Either way the type is fixed from then on, and a later line can't store text in n or a number in s.

    Declared types are enforced

    Scala checks types when it compiles the program. If you write the type after the name, the value on the right has to be of that type. Here is the lesson's starting code:

    val age: Int = "29"
    
    val phone: String = 2025550153
    

    Both lines are rejected, and the compiler explains each one:

    -- [E007] Type Mismatch Error ---------------------------------------------
      |  val age: Int = "29"
      |                 ^^^^
      |                 Found:    ("29" : String)
      |                 Required: Int
    
    -- [E007] Type Mismatch Error ---------------------------------------------
      |  val phone: String = 2025550153
      |                      ^^^^^^^^^^
      |                      Found:    (2025550153 : Int)
      |                      Required: String
    

    Found is the type the value has and Required is the type the declaration asked for. Scala does not quietly turn the text "29" into a number, nor the number into text.

    Fixing the exercise

    1. Keep the type annotations. Int and String state what each value is supposed to be, and they are correct.
    2. Fix age. An age is a quantity you may calculate with, such as next year's age, so it is a number. Remove the quotes: 29.
    3. Fix phone. A phone number is an identifier. You never add two phone numbers, and a leading zero or a plus sign would be part of it. Put it in quotes so it is text.

    The solution in Scala

    val age: Int = 29
    
    val phone: String = "2025550153"
    

    A good rule for choosing: if you might do arithmetic with the value, use a number. If it only names or identifies something, use a string.

    Gotchas

    • Digits in quotes are still text. "29" + 1 is "291", not 30.
    • Not every number fits in an Int. An Int holds values up to 2147483647. Longer numbers need a Long, written with an L, as in 12025550153L. Storing a phone number as text avoids the question.
    • Multiplying strings. "ab" * 3 repeats the text and gives "ababab", but "ab" * "cd" is a type error, because the right side of * has to be an Int.
    • Converting is explicit. To get a number out of a string, call a method such as "29".toInt. A later lesson covers conversion.

    Key takeaways

    • No quotes means a number, double quotes mean a String.
    • A declared type must match the value, and the compiler reports Found and Required types when it doesn't.
    • Use numbers for quantities you calculate with and strings for identifiers such as phone numbers.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free