scalabeginner

Scala Hello World Application

Create a basic Scala application with main method, string interpolation, and val/var basics.

scala
// Scala 3 syntax
@main def hello(): Unit =
  val name = "World"
  println(s"Hello, $name!")

  // Val (immutable) vs Var (mutable)
  val immutable = 42        // cannot be reassigned
  var mutable = 10          // can be reassigned
  mutable = 20

  // String interpolation
  val x = 10
  val y = 20
  println(s"$x + $y = ${x + y}")

  // f-interpolator for formatting
  val pi = 3.14159
  println(f"Pi is $pi%.2f")

  // Raw strings
  val path = raw"C:\Users\name\docs"
  println(path)

  // Multi-line strings
  val json = s"""|
    |{
    |  "name": "$name",
    |  "value": $x
    |}""".stripMargin
  println(json)

Use Cases

  • Getting started with Scala
  • Understanding val vs var immutability
  • String formatting and interpolation

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.