Kotlin Variables

A variable refers to a memory location that stores some data.
You can declare variables in kotlin using val and var keywords.

val:
Variable declared using val keyword is immutable(read only). It can not be reassigned after it is initialized.

val name = "Kotlin"
name = "Language" //Error: val can not be reassigned


It is similar to final variable in Java.

var:
Variable declared using var keyword is mutable. It can be reassigned after it is initialized.

val name = "Kotlin"
name = "Language" //No error. It works

It is similar to regular variable in Java.


Type inference:
In Kotlin, it is not mandatory to explicitly specify the type of variable that you declare. Kotlin compiler will automatically infer the type of the variable from the initializer section.

val name = "Kotlin" // type infered as "String"
val value = 100 //type infered as "Int"

You can also explicitly define the type of the variable.
val name: String = "Kotlin"
val value: Int = 100

Type of the variable declaration mandatory if you dont initialize the variable.

val name // Error: variable must either have a Type annotation or be initialized
name = "Kotlin"

You can also declare something like this.
val name: String //works
name = "Kotlin"

Comparison to Java Programming Language

Kotlin fixes some of the Java issues.
  • Null references are controlled by the type system.
  • No raw types
  • Arrays in Kotlin are invariant
  • Kotlin has proper function types, as opposed to Java's SAM-conversions
  • Use-site variance without wildcards
  • Kotlin does not have checked exceptions

Java has below that kotlin does not have

  • Checked exceptions
  • Primitive types that are not classes
  • Static members
  • Non-private fields
  • Wildcard-types
  • Ternary-operator a ? b : c

What Kotlin has that Java does not
  • Lambda expressions + Inline functions
  • Extension functions
  • Null-safety
  • Smart casts
  • String templates
  • Properties
  • Primary constructors
  • First-class delegation
  • Type inference for variable and property types
  • Singletons
  • Declaration-site variance & Type projections
  • Range expressions
  • Operator overloading
  • Companion objects
  • Data classes
  • Separate interfaces for read-only and mutable collections
  • Coroutines

Getting started with Kotlin on Android Studio 3.0 +

Kotlin is fully supported in Android Studio 3.0 and higher, so it's easy to create new projects with Kotlin files, add Kotlin files to your existing project, and convert Java language code to Kotlin. 

You dont need to setup any kotlin plugins in Android studio 3.0 and higher.

You can then use all of Android Studio's existing tools with your Kotlin code, such as autocomplete, lint checking, refactoring, debugging, and more.

step 1:
 To create project in Kotlin, need to select "Include Kotlin support" checkbox in below screen.


step 2:
Here, you need to select target sdk version. This step is same as Android/Java project.


step 3:
Enter file name. This step is same as Android/Java project.


step 4:
Project setup is done. see below project structure. Kotlin/Android project structure is same as android/Java project. Only Class file name extension will be in .kt 


step 5:
Follow below option to add new kotlin class to existing project.


What is Kotlin?

Kotlin is a statically-typed open-source programming language. It was developed by Jet Brains. 

Kotlin is great fit for developing Android applications, Server side applications, for java script and for Native development.

Kotlin is free. It is developed under the Apache 2.0 license and source code is available on GitHub.

Kotlin is an officially supported language for developing Android apps, along with Java. Google announced support for Kotlin in Android at Google I/O 2017.

Kotlin is supported by all major Java IDEs including IntelliJ IDEA, Android Studio, Eclipse and NetBeans.

Kotlin has both object-oriented and functional constructs. You can use it in both OO and FP styles, or mix elements of the two.

With first-class support for features such as higher-order functions, function types and lambdas, Kotlin is a great choice if you’re doing or exploring functional programming.