← Scala EnglishChapter 01 of 13

Introduction to Scala

## Learning Objectives - Understand Scala history and design goals - Set up Scala development environment - Write and run your first Scala program - Understand Scala syntax basics ## What is Scala? Scala is a modern multi-paradigm programming language that combines object-oriented and functional programming. The name "Scala" derives from "scalable language." ```scala object HelloWorld extends App { println("Hello, World!") } ``` ## Scala Platform ### Key Features - **Unified Type System** - Everything is an object, including functions - **Immutability** - Encourages immutable data structures - **Type Inference** - Compiler infers types when not explicitly specified - **Functional First** - Functions are first-class citizens - **JVM Interoperability** - Runs on JVM and interoperates with Java ### Platform Components | Component | Description | |-----------|-------------| | scala | Scala REPL and runner | | scalac | Scala compiler | | sbt | Scala build tool | | Scala REPL | Interactive Scala shell | ## Installing Scala ### Download Get Scala from ### Using SDKMAN (Recommended) ```bash curl -s "https://get.sdkman.io" | bash sdk install scala ``` ### Verify Installation ```bash scala -version scalac -version ``` ## Your First Program ### File: HelloWorld.scala ```scala object HelloWorld extends App { println("Hello, World!") } ``` ### Compile and Run ```bash scalac HelloWorld.scala # Creates HelloWorld.class scala HelloWorld # Runs the program ``` ### Using scala Command Directly ```bash scala -e 'println("Hello, World!")' ``` ## Scala Program Structure ```scala object ClassName { // Singleton object (entry point) def main(args: Array[String]): Unit = { // Statements go here } } ``` ### Alternative Entry Point (Scala 2.9+) ```scala object ClassName extends App { println("Hello, World!") // args is available via App trait } ``` ## Comments ```scala // Single-line comment /* * Multi-line comment */ /** * Documentation comment */ ``` ## The main Method ```scala object Main { def main(args: Array[String]): Unit = { args.foreach(println) } } ``` ## System Output ```scala // Print with newline println("Hello") // Print without newline print("Hello ") print("World") // String interpolation (Scala 2.10+) val name = "Alice" val age = 30 println(s"Name: $name, Age: $age") println(s"Age in 5 years: ${age + 5}") ``` ## Command Line Arguments ### Hello.scala ```scala object Hello extends App { if (args.length > 0) { println(s"Hello, ${args(0)}") } else { println("Hello, World!") } } ``` ```bash scalac Hello.scala scala Hello Alice # Output: Hello, Alice ``` ## Using the Scala REPL The Scala REPL (Read-Eval-Print Loop) is an interactive shell: ```bash scala ``` ```scala scala> val x = 10 x: Int = 10 scala> val y = 20 y: Int = 20 scala> x + y res0: Int = 30 scala> def greet(name: String) = s"Hello, $name" greet: (name: String)String scala> greet("World") res1: String = Hello, World ``` ## IDEs ### Popular IDEs - IntelliJ IDEA with Scala plugin (recommended) - VS Code with Metals (Scala language server) - Ammonite REPL for scripting - Metals with VS Code or Neovim ## Scala Versions ```bash scala -version ``` | Version | Year | Feature | |---------|------|---------| | Scala 2.10 | 2013 | String interpolation, Reflection | | Scala 2.11 | 2014 | Modularization | | Scala 2.12 | 2016 | Java 8 compatibility | | Scala 2.13 | 2019 | Collections redesign | | Scala 3.0 | 2021 | New syntax, dependent types, metaprogramming | ## Scala vs Java | Aspect | Java | Scala | |--------|------|-------| | Type inference | Limited (var in Java 10+) | Extensive | | Functions | Objects (Java 8+) | First-class functions | | Extension methods | Via utilities | Implicit classes | | Traits | Interfaces with default methods | Rich traits with state | | Pattern matching | switch (basic) | Powerful pattern matching | | Null handling | Null pointer exceptions | Option type | ## Summary - Scala combines object-oriented and functional programming - Runs on the JVM with full Java interoperability - Type inference reduces verbosity - Everything is an object, including functions - REPL enables interactive development - Use `scalac` to compile and `scala` to run - Comments: `//`, `/* */`, `/** */`

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →