Kotlin Easy Basics 2 – Data Types

kotlin easy basics tutorial by grauenwald.com

Welcome back to the second Easy Basics for Kotlin. Now you will learn about about Variables and basic Data Types that Kotlin supports for creating Variables.

  • If you didn’t read the first part Kotlin Easy Basics 1 — Click here

Variables

A variable is a location in memory where data is stored. Furthermore a variable needs a unique name (identifier).

To declare a variable you either use var or val before the unique name!

fun main(args : Array<String>) {
	var name = "Maxim" // String
	val age = 26      //  Int
}
  • var – (Immutable reference) – A variable declared using var is immutable, so it can’t be changed after a value was assigned
  • var – (Mutable reference) – A variable declared using val is mutable, so it can be changed later in the code.

In the previous code name is a variable from type String and age is a variable from the type Int. You can also make clearer in code if a variable String or Int is.

fun main(args : Array<String>) {
	var name: String = "Maxim" // String
	val age: Int  = 26        //  Int
}

You can also assign the value to a variable later after the declaration in code.

fun main(args : Array<String>) {
  var name: String      // variable declaration - String
  name = "Maxim"        // variable initialization

  val age: Int          // variable declaration - Int
  age = 26

  name = "theropoy"     // variable re-initialization
}

Data Types

String/Char

  • String
  • Char

Number

  • Byte
  • Short
  • Int
  • Long
  • Float
  • Double

Boolean

  • true
  • false