HOME
ABOUT

Scala Basics

Values, Variables, and Types

In Scala, you can declare values and variables. Values are immutable (cannot be changed), while variables are mutable (can be changed).

// Declaring a value
val x: Int = 10

// Declaring a variable
var y: String = "Hello"

// Type inference
val z = 3.14 // Scala infers that z is a Double

Basic Data Types

Scala has several built-in data types, including:

  • Int: For integers (e.g., 1, -5, 1000)
  • Double: For floating-point numbers (e.g., 3.14, -2.5)
  • Boolean: For true/false values
  • String: For text

Basic Operations and Expressions

Scala supports standard arithmetic and logical operations.

val sum = 5 + 3
val product = 4 * 2.5
val isEqual = (5 == 5)

String Interpolation

String interpolation allows you to embed variables directly into strings.

val name = "Alice"
val greeting = s"Hello, $name!"

Console Input/Output

You can print to the console using println and read input using scala.io.StdIn.readLine.

println("Enter your name:")
val input = scala.io.

Related Articles

  • Introduction to Scala
  • Scala Setup
  • Scala Basics
  • Scala Variables and Data Types
  • Control Structures
  • Functions
  • Scala Functions and Methods
  • Collections
  • Scala Classes and Objects
  • Object-Oriented Programming
  • Scala Inheritance and Polymorphism
  • Functional Programming Concepts
  • Scala Collections
  • Pattern Matching
  • Scala Pattern Matching
  • Implicits
  • Scala Implicits
  • Concurrency and Parallelism
  • Scala Concurrency and Parallelism
  • Build Tools and Dependency Management
  • Scala Build Tools and Dependency Management
  • Testing in Scala
  • Working with External Systems
  • Advanced Topics
  • Practical Projects
  • More Articles...