Sharing our technical trials in terms of Ubuntu software installs, Blogger hacks, Android and Kotlin learning.
More on variables – Kotlin Tutorials for Beginners
More on variables – Kotlin Tutorials for Beginners

More on variables – Kotlin Tutorials for Beginners

In this post we will explore more on initialization, names and constraints related to variable types.

Types are covered in our post Types of variables.

I. Variables & Initialisation

1. Assigning a initial value to a variable is called initialization

  • In Kotlin if a variable is not initialized before using, compiler throws an error.
Example :
 fun main (args:Array<String>)
{
      var x
      println("Value of x is "+x)
}
Output :

Compilation error “This variable must either have a type annotation or be initialized”

Explanation :

var x , is called declaration a variable

var x = 6, is called initialization wherein a value is given to a variable

In Kotlin, the compiler get’s upset 😉 and throws error if you declare a variable and don’t initialize it.

II. Variables & Name

1. Name of the variable cannot be repeated

Example :
 fun main (args:Array<String>)
{

      var x = 10
      var x = 20
      println("Value of x is "+x)
}
Output :

Compilation error “Conflicting declarations: var x: Int, var x: Int”

Explanation :

Name “x” is already used for a variable, trying to use the same name again gives compilation error.

2. Reserved words cannot be used for variable names

Example :
 fun main (args:Array<String>)
{
      var while = 6
      println("Value of while is "+while)
}
Output :

Compilation error “Expecting property name or receiver type”

Explanation :

There are some words in Kotlin called as reserved words which indicate certain actions. Using these as variable names gives error.

For instance the word “While” is used to indicate looping, using this as a variable name gives compile time error.

III. Variable Type

1. Type can be defined implicitly or explicitly

  • Implicit – Variable is given a value, the compiler infers the type based on the value assigned.
  • Explicit – Type is mentioned in the declaration, the compiler is asked to make sure the variable is of a certain type.
Example :
 fun main (args:Array<String>)
{

      var x = 10
      var y : Int = 20
      println("Value of x is "+x)
      println("Value of y is "+y)
}
Output :

“Value of x is 10”
“Value of y is 20”

Explanation :

Both are valid ways of declaring a variable

Implicit declaration -> var x = 10, type is inferred based on the value

Explicit declaration -> var y : Int = 20, type Int is explicitly mentioned

2. While explicitly mentioning type, the value assigned should be compatible to the type

Example :
 fun main (args:Array<String>)
{

      var x : Int = 10.2
      println("Value of x is "+x)
}
Output :

“The floating-point literal does not conform to the expected type Int”

Explanation :

Variable declared is of type Int, value assigned is of type float therefore compiler throws error

3. If a value assigned is too large for the declared type compiler throws an error

Example :
 fun main (args:Array<String>)
{

      var x : Byte = 500
      println("Value of x is "+x)
}
Output :

“The integer literal does not conform to the expected type Byte”

Explanation :

Variable is declared as Byte, as the value assigned is too large for Byte type compiler throws error

4. Assigning one variable to another can be done if types are compatible

Example 1 :
 fun main (args:Array<String>)
{

      var x = 10
      var y = x

      println("Value of x is "+x)
      println("Value of y is "+y)
}
Output :

“Value of x is 10”
“Value of y is 10”

Explanation :

Based on the value assigned an Int object is created with value 10 and the reference of this is held by variable “x”

“x” is then assigned to “y”

As a result “y” will also be of type Int and hold reference to the same object.

Example 2:
 fun main (args:Array<String>)
{

      var x = 10
      var y: Long = x

      println("Value of x is "+x)
      println("Value of y is "+y)
}
Output :

Kotlin: Type mismatch: inferred type is Int but Long was expected

Explanation :

Based on the value assigned an Int object is created with value 10 and the reference of this is held by variable “x”

later “x” is assigned to “y”

“x” is of type Int

“y” is of type Long

As of a result of type mismatch the compiler throws error.

For more on how to assign a value of one type to another check out our post on Type Casting .