Kotlin Variable

Variable is Container to store values, every variable should be declared before using otherwise it gives syntax errors.

variable name should be declared using lowerCamelCase.

Variable declaration With initialization: If variable is declare with initialization there is no need to specify its data type

fun main() {
    // variable declaration and initialization
    var color = "Green"
    val name = "Coding Friction"
    println(color)
    println(name)
}

To declare variables use var or val keyword and assign a value with (=) equal operator.

Declaration Without Initialization: Variable can be declare without initializing it if you specify its data type

var uninitializedVariable: Int // Declaration without initialization (compiles only for nullable types)
uninitializedVariable = 42 // Initialized later

Nullable Variable: by default, variables in Kotlin are non-nullable(variable is one that cannot hold a null value.). If a variable value can be null, you can declare it as nullable by appending ? to the type:

fun main() {
   val nonNullable: String = "John"
   //nonNullable = null // get error
   var nullableVariable: String? = null
   print(nullableVariable)//null
}

In this example if you assign null value to nonNullable variable, you will get an error.

In Kotlin, variables are declared using the val (immutable) or var (mutable) keyword. Here’s a brief explanation of both:

var (Mutable Variable) : Variable declare with var keyword value can be changed later.

fun main(){
    var name: String = "John"

    // change the variable value
    name = "Marsh"
    println(name) // Marsh
}

val (Immutable Variable): The value assigned to a val cannot be changed once it is assigned. here is an example:

fun main(){
    val name: String?
    name = "Smith" // Assigned at runtime
//     name = "Marsh"// This line will cause a compilation error

    println(name) // Smith
    val name1 = getName()
    println(name1) // Hello world
}
fun getName():String{
    return "Hello world"
}

In this example, we are initializing value at runtime using getName() function

Scope of a variable –

The scope of a variable defines the scope of the code where the variable is accessible or visible.

1. Local Scope:

Variable can be accessed only inside the block of code({….}) or function where its declared, you can not access outside the block.

fun exampleFunction() {
    val localVar = 12 // localVar is in local scope
    // ...
}
// localVar is not accessible here

2. Top-Level Scope: Variables are declared outside any class or function have top-level scope. That variables are accessible globally within the file where they are defined.

val topLevelVar = "Global" // topLevelVar is in top-level scope

fun exampleFunction() {
    println(topLevelVar) // Accessing topLevelVar within a function
}

3.Member Scope: Member variables are accessible throughout the class. Their scope is limited by the class’s boundaries.

class ExampleClass {
    val memberVar = "Hello" // memberVar is in member scope

    fun exampleFunction() {
        println(memberVar) // Accessing memberVar within a function
    }
}

Leave a Reply