Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Declaring Variables in Kotlin | Kotlin Variables and Data Types
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
Introduction to Kotlin

bookDeclaring Variables in Kotlin

Understanding how to declare variables is one of the first steps to writing useful Kotlin code. In Kotlin, you declare variables using either val or var. The keyword you choose determines whether the value stored in the variable can be changed after it is first assigned.

A variable declared with val is immutable. This means that once you assign a value to a val, you cannot change it—trying to do so will result in a compilation error. Use val when you want to make sure the value does not change, like for constants or values that should remain the same throughout your program.

A variable declared with var is mutable. This means you can assign a new value to it at any time after its initial declaration. Use var when you expect the variable's value to change as your program runs.

Main.kt

Main.kt

copy
12345678910111213141516171819
package com.example fun main() { // Immutable variable using val val greeting: String = "Hello, Kotlin!" println(greeting) // Mutable variable using var var counter: Int = 1 println("Counter is: $counter") // Changing the value of counter counter = 2 println("Counter is now: $counter") // Uncommenting the next line would cause a compilation error // greeting = "Hi, Kotlin!" // Error: Val cannot be reassigned }

In the code above, you see both val and var in action. The greeting variable is declared with val, so its value cannot change after it is set. This is why trying to assign a new value to greeting would cause an error. The counter variable is declared with var, so you can change its value as many times as you need. This flexibility makes var useful for values that need to be updated, such as counters, accumulators, or any variable whose value is expected to change during the program's execution.

As a rule of thumb, prefer val unless you have a clear reason for a variable to be mutable. This helps you write safer and more predictable code, as you can be confident that immutable values will not change unexpectedly.

question mark

Which statement about val and var in Kotlin is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookDeclaring Variables in Kotlin

Swipe um das Menü anzuzeigen

Understanding how to declare variables is one of the first steps to writing useful Kotlin code. In Kotlin, you declare variables using either val or var. The keyword you choose determines whether the value stored in the variable can be changed after it is first assigned.

A variable declared with val is immutable. This means that once you assign a value to a val, you cannot change it—trying to do so will result in a compilation error. Use val when you want to make sure the value does not change, like for constants or values that should remain the same throughout your program.

A variable declared with var is mutable. This means you can assign a new value to it at any time after its initial declaration. Use var when you expect the variable's value to change as your program runs.

Main.kt

Main.kt

copy
12345678910111213141516171819
package com.example fun main() { // Immutable variable using val val greeting: String = "Hello, Kotlin!" println(greeting) // Mutable variable using var var counter: Int = 1 println("Counter is: $counter") // Changing the value of counter counter = 2 println("Counter is now: $counter") // Uncommenting the next line would cause a compilation error // greeting = "Hi, Kotlin!" // Error: Val cannot be reassigned }

In the code above, you see both val and var in action. The greeting variable is declared with val, so its value cannot change after it is set. This is why trying to assign a new value to greeting would cause an error. The counter variable is declared with var, so you can change its value as many times as you need. This flexibility makes var useful for values that need to be updated, such as counters, accumulators, or any variable whose value is expected to change during the program's execution.

As a rule of thumb, prefer val unless you have a clear reason for a variable to be mutable. This helps you write safer and more predictable code, as you can be confident that immutable values will not change unexpectedly.

question mark

Which statement about val and var in Kotlin is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt