Variable declaration in Kotlin

Published by inkskull on

Checkout the variable declaration in kotlin


Kotlin is a general-purpose, statically typed, and open-source programming language. It runs on JVM and can be used anywhere Java is used today. It can be used to develop Android apps, server-side apps and much more. Google accepetd the kotlin as the officill language for android.


Kotlin was developed by JetBrains team. A project was started in 2010 to develop the language and officially, first released in February 2016. Kotlin was developed under the Apache 2.0 license.


  • Concise: Kotlin reduces writing the extra codes. This makes Kotlin more concise.
  • Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the NullPointerException (null reference) from the code.Interoperable.
  • Interoperable: Kotlin easily calls the Java code in a natural way as well as Kotlin code can be used by Java.
  • Smart cast: It explicitly typecasts the immutable values and inserts the value in its safe cast automatically.
  • Compilation Time: It has better performance and fast compilation time.
  • Tool-friendly: Kotlin programs are build using the command line as well as any of Java IDE.
  • Extension function: Kotlin supports extension functions and extension properties which means it helps to extend the functionality of classes without touching their code.

Kotlin variable is declared using keyword var and val.

  1. var language =“kotlin”
  2. val salary = 56777

The difference between var and val is

  • var (Mutable variable): We can change the value of variable declared using var keyword later in the program.
  • val (Immutable variable): We cannot change the value of variable which is declared using valkeyword.

Here, variable language is String type and variable salary is Int type. We don’t require specifying the type of variable explicitly. Kotlin complier knows this by initilizer expression (“kotlin” is a String and 56777

is an Int value). This is called type inference in programming.


Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By :