Kotlin Tutorial

Kotlin Classes

Kotlin Data Types:

In this chapter you’ll learn about Data Types of Kotlin.
In Kotlin, the type of a single variable can be declared according to its value.

				
					val number = 10 // Int
val doubleNumber = 5.91 // Double
val bool = true // Boolean
val singleLetter = 'A' // Char
val text = "Hello World" // String
				
			

In the previous chapter you learned about specifying types which will result the same.

				
					val number : Int = 10 // Int
val doubleNumber : Double = 5.91 // Double
val bool : Boolean = true // Boolean
val singleLetter : Char = 'A' // Char
val text : String = "Hello World" // String
				
			

It depends upon the situation. Sometimes we need to specify types and sometimes not.
The built-in types in Kotlin can be categorized as:

  • Numbers
  • Characters
  • Booleans
  • Strings 
  • Arrays

 

Numbers:

In Kotlin, there are about 6 built-in types representing numbers.

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

Byte:

Byte data type can store values from -128 to 127. This Data Type can be used instead of Integer to save memory only in case, when you are certain that the value of the variable will be between [-128, 127].

Example:

				
					fun main(args: Array<String>) {
   val byteVar : Byte = 115
   println(byteVar)
}

				
			

The code above outputs:
115
NOTE: If you pass the value which is greater than 127 or less than -128 this will throw an error (value out of range)

Short:

Short data type can store values from -32768 to 32767. This Data Type can also be used instead of Integer to save memory only in case, when you are certain that the value of the variable will be between [-32768, 32767].

Example:

				
					fun main(args: Array<String>) {
   val shortVar : Short = 30000
   println(shortVar)
}

				
			

The code above outputs:
30000
NOTE: If you pass the value which is greater than 32767 or less than -32768 this will throw an error (value out of range).

Int:

Int data type can store values from -2,147,483,648 to 2,147,483,647.

Example:

				
					fun main(args: Array<String>) {
   val intVar : Int = 1000000
   println(intVar)
}


				
			

The code above outputs:

1000000

If you assign an integer value between (-2,147,483,648 to 2,147,483,647) to a variable without explicitly specifying its type, the variable type will be Int type. 

Example:

				
					fun main(args: Array<String>) {
   val intVar = 100
   println(intVar)
}


				
			

NOTE: If you pass the value which is greater than 2,147,483,647 or less than -2,147,483,648 this will throw an error (value out of range).

Long:

There are two ways to specify that the variable is of type Long.

Long data type can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Way 1:

Specify the type explicitly to a variable.

Example:

				
					fun main(args: Array<String>) {
   val longVar : Long = 98989
   println(longVar)
}


				
			

The code above outputs:

98989

Way 2:

Use the capital L at the end of the number.

Example:

				
					fun main(args: Array<String>) {
   val longVar = 98989L
   println(longVar)
}


				
			

The code above outputs:

98989

NOTE: If you pass the value which is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808 this will throw an error

Double & Float:

In Kotlin, there are two primitive floating point types:

  • Float 
  • Double

Floating point types represent decimal numbers, such as 8.91 or 5.4321.

Float Example:

				
					fun main(args: Array<String>) {
   val floatVar: Float = 3.0F
   println(floatVar)
}



				
			

The following code outputs:

3.0

Remember: The “F” at the end of the number. You must put this while specifying that it is a Float variable.

Double Example:

				
					fun main(args: Array<String>) {
   val doubleVar: Double = 23.0
   println(doubleVar)
}



				
			

The code above outputs:

23.0

Similarly, without specifying the type explicitly, you can know what the type of a variable is. 

Float Example:

				
					fun main(args: Array<String>) {
   val floatVar = 23.0F
   println(floatVar)
}



				
			

Double Example:

				
					fun main(args: Array<String>) {
   val doubleVar = 23.0
   println(doubleVar)
}



				
			

Scientific Numbers:

Floating point numbers can also be scientific numbers by putting “E” or “e” to indicate the power of 10.

				
					fun main(args: Array<String>) {
   val n1 = 9E3
   val n2 : Float = 23E3F
   val n3 : Double = 52E2
   println(n1)
   println(n2)
   println(n3)
}



				
			

The following code outputs:

9000.0

23000.0

5200.0

Characters:

The data type Char is used to store a single character. The value must go inside single quotes ‘A’, ‘B’, or ‘c’ etc.

Example:

				
					fun main(args: Array<String>) {
   val characterVar : Char = 'A'
   println(characterVar)
}



				
			

The code above outputs:

A

Special characters start from a backslash \ . The following escape sequence characters are supported in Kotlin.

  • \t  –  tab
  • \n  –  new line 
  • \b  –  backspace
  • \r  –  carriage return
  • \’  –  single quotation mark
  • \”  –  double quotation mark
  • \\  – backslash
  • \$  –  dollar sign

NOTE:  Like in Java Programming language you cannot use ASCII values to display certain characters, for example:

The value 70 would output “F”, but in Kotlin this will throw an error (The integer literal does not conform to the expected type Char).

				
					fun main(args: Array<String>) {
   val singleLetter : Char = 70
   println(singleLetter)
}