Saturday, 4 April 2020

Variables In Kotlin

Variable Declaration In Kotlin

Mutable Variables - var


We can declare the mutable variable by using 'var' in the Kotlin programming language. We don't need to declare the type of the variable in the Kotlin programming language, It inferred the type when we initialize the variable. 

We must declare and initialize the variable at the same type to get the benefit, Otherwise, we have to provide the type of the variable at the time of declaration. If we'll try to declare the variable without initializing or providing the type then we'll get Syntax Error.

Some examples here:

// The variable must either have a type annotation or be initialized
var a   
        
// Immediate assignment of the variable
var a = 2

// Int type is inferred, no need to specify the type of variable
var a = 2     

//  Type required when no initializer is provided to the variable
var b: Int  

//  deferred assignment of the variable 
b = 3           

//  Reassigned the value
a = 5, b = 8  

Immutable Variables - val


These are Read Only variables and used to refer only one object. When we assign the value to these variables, we can not change or re-assign the value to these variables. These variables are used for assign-once only.

These variables are not constant. It can be initialized with the value of a variable and its value doesn't necessary to be known at compile time. if it's declared inside a construct that is invoked repeatedly like a function or loop, It can take a different value every time on each invocation.

These variables can not be re-assigned while it's in a scope. Also, these variables can refer to mutable objects like mutable collections.

Some examples here:

// Immediate assignment of the variable
val a = 2               

// String type is inferred, no need to specify the type of variable
val st = "String"  

//  Type required when no initializer is provided to the variable
var b: Int      

//  deferred assignment of the variable         
b = 3                 

// Int type is inferred, no need to specify the type of variable      
val num = 2           

// Can not reassign, Compile Time Error: Val can not be reassigned
num = 7                


Constants


These variables are truly constant variable, We can not re-assign the value to these variables like read-only variables (val). The difference is that its value should be known at compile-time necessarily. We can only declare these variables at the top level of a file or inside an object declaration (Singleton class in Kotlin), but not inside a class declaration.

Some examples here:

// Constant variable declaration
const val a = 9      

// Compile Time Error: Property must be initialized
const val b      

// Can not reassign       
a = 5                      

I think, Now we have enough knowledge of variables in Kotlin.

Happy Learning!!


1 comment:

Visibility Modifier In Kotlin

Visibility Modifier The visibility modifier basically defines a scope from where it can be visible and accessible. There are four visi...