0Pricing
Scala for Backend Engineering & Functional Programming · Ders

val ve var

Değişmez ve değiştirilebilir bağlamalar.

val ve var, CoddyKit'te ücretsiz bir Scala for Backend Engineering & Functional Programming dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Scala for Backend Engineering & Functional Programming öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Two Ways to Name Values

In Scala, you bind a name to a value using either val or var.

val creates an immutable binding: once assigned, it can never point to another value. var creates a mutable binding that you can reassign later.

Choosing between them is one of the first habits a Scala backend engineer builds.

val name = "Ada"
var score = 10

Defining a val

A val is read-only. You assign it once, and the compiler guarantees it stays the same.

This makes your code easier to reason about: a val never surprises you with a changed value somewhere else.

object Main extends App {
  val pi = 3.14
  println(pi)
}

Reassigning a val Fails

Trying to reassign a val is a compile error, not a runtime crash. The compiler stops you before the program ever runs.

The snippet below would fail with reassignment to val.

val country = "Turkey"
country = "Spain" // error: reassignment to val

Defining a var

A var can be reassigned as many times as you like, as long as the new value has a compatible type.

Use var when a value genuinely needs to change over time, such as a counter in a loop.

object Main extends App {
  var count = 0
  count = count + 1
  count = count + 1
  println(count)
}

var Keeps Its Type

Even though a var can be reassigned, its type is fixed at definition.

If level starts as an Int, you cannot later store a String in it. The compiler rejects the mismatch.

var level = 1
level = 5        // ok, still an Int
level = "high"  // error: type mismatch

Prefer val by Default

Idiomatic Scala favors val almost everywhere. Immutability avoids whole classes of bugs and makes concurrent code safer.

  • Reach for val first.
  • Switch to var only when you truly need mutation.

Rewriting var as val

Many loops that look like they need a var can be expressed with a val and an expression instead.

Here a sum is computed with a range and sum, no mutation required.

object Main extends App {
  val total = (1 to 5).sum
  println(total)
}

Scope of a Binding

Both val and var live inside the block { ... } where they are defined.

A binding declared inside a block is not visible outside of it. This keeps names tidy and prevents accidental reuse.

object Main extends App {
  val outer = 1
  {
    val inner = 2
    println(outer + inner)
  }
  // inner is not visible here
}

Lazy val

A lazy val is computed only the first time it is used, then cached.

This is handy for expensive values you might not always need. The body runs at most once.

object Main extends App {
  lazy val greeting = { println("computing"); "Hi" }
  println("before")
  println(greeting)
  println(greeting)
}

val with Explicit Type

You can annotate a binding's type after a colon. This documents intent and can widen the type on purpose.

Here amount is declared as a Double even though 10 looks like an Int.

val amount: Double = 10
val label: String = "price"

val vs var Side by Side

The two behave the same until you try to reassign.

  • val x: assign once, fixed forever.
  • var y: assign and reassign freely.

The program below increments only the var.

object Main extends App {
  val base = 100
  var bonus = 0
  bonus = bonus + 25
  println(base + bonus)
}

Quick Check

Test your understanding of val and var.

Recap

You learned the two ways to bind names in Scala.

  • val is immutable and is the default choice.
  • var is mutable and reserved for values that must change.
  • Reassigning a val is a compile error.
  • Types are fixed for both; lazy val defers computation.

Sıkça Sorulan Sorular

“val ve var” dersi ücretsiz mi?

Evet — “val ve var” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Scala for Backend Engineering & Functional Programming kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.

“val ve var” dersinde ne öğreneceğim?

Değişmez ve değiştirilebilir bağlamalar. Scala for Backend Engineering & Functional Programming ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Scala for Backend Engineering & Functional Programming öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Scala for Backend Engineering & Functional Programming, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“val ve var” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Scala for Backend Engineering & Functional Programming dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Scala for Backend Engineering & Functional Programming dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. val ve var
  2. Temel Türler ve Değişmezler
  3. Tür Çıkarımı
  4. İfadeler ve Bildirimler
← Scala for Backend Engineering & Functional Programming Sayfasına Dön