How do I declare variables and use string interpolation in an EK9 program?

← Getting Started · Ref: Q872

Declare variables with '<-' (type is inferred). Use backtick strings with ${expression} for interpolation. EK9 infers the type from the assigned value — no need to declare types explicitly for locals.

See Q37 for strings. See Q22 for variable declaration. Use 'ek9 -h String' for string methods.

Example

defines module qa.getting.started.variables

  defines program

    VariableDemo()
      stdout <- Stdout()

      greeting <- "Hello"
      userName <- "Steve"
      age <- 42

      stdout.println(`${greeting}, ${userName}!`)
      stdout.println(`You are ${age} years old`)
Other ways to ask this
  • Show me variable declaration with interpolated strings in EK9
  • How do backtick strings work in EK9?
  • EK9 string templates with variables

Coming from another language?

Java: var keyword (Java 10+). Python: dynamic typing. Kotlin: val/var. EK9: '<-' with type inference.

Keywords: backtick, declaration, string, template, inference, variable, interpolation