In this topic we cover:

  • Introduction to basic data types.
  • Literal constants in Kotlin.
  • Using underscores in number literals.
  • Explicit number conversions in Kotlin.


Both Kotlin and Java are object oriented languages though Kotlin also provides full support to functional programming. However, one major difference between Kotlin and Java is that everything in Kotlin is an object, unlike Java. Java has special primitive types that aren't objects. Thus don't natively support method calls, generics and cannot be assigned null. These include int,boolean etc.
To work with them as objects you have to use the wrapper objects from java.lang package like java.lang.Integer and java.lang.Boolean.
There is no need of this in Kotlin, those so called primitives are full objects in Kotlin. Sometimes, the Kotlin compiler maps those basic types to JVM primitives to boost performance.
The basic types in Kotlin include:

  1. Numbers.
  2. Booleans.
  3. Chars.
  4. Strings.
  5. Arrays.


Kotlin has the following built-in number types:
| Type | Width |
| Byte ----- | 8 |
| Short ---- | 16 |
| Int ---------| 32 |
| Long ----- | 64 |
| Float ------| 32 |
| Double ---| 64 |

Literal Constants

Literal constants for integral values in Kotlin comprise:

  • Decimals : 123. Longs are tagged by a capital L : 123L
  • Hexadecimals: 0x0F
  • Binaries: 0b00001011

Note : Octal literals are not supported in Kotlin.
There is also a conventional notation for floating point numbers:

  • Doubles by default: 123.5
  • Floats are tagged by or F: 123.5F

Example numbers declarations and initializations:

val int = 222
val long = 222545L
val double = 25.68
val float = 25.68F
val hexadecimal = 0xAB
val binary = 0b01010101

Take note the following:

  1. We suffix our Float with f while we suffix Long with L.
  2. Floating point numbers use double by default.
  3. Hexadecimal numbers are prefixed with 0x while binary numbers are prefixed with 0b.
  4. Kotlin won't automatically widen for you numbers. You have to explicitly cast one number type to the other.

Underscores in Number Literals

Kotlin allows you to use literals to make number constants more readable.

val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010

Explicit Number Conversions in Kotlin

Unlike in java. There is no implicit widening of numbers in Kotlin. You convert numbers explicitly using predefined methods.
The methods include:

  1. toByte()
  2. toShort()
  3. toInt()
  4. toLong()
  5. toFloat()
  6. toDouble()
  7. toChar().

Examples of explicit casting of numbers in kotlin:

//Integer to long
val num1 = 222
val long = num1.toLong()

//Float to Double
val num2 = 25.68
val double = num2.toDouble()


Booleans are used to present true or false as you would expect.

var old=true
var young=false


Chars represent a single character. Chars in kotlin use single quote.

var letter='a'

Chars also support escaping for \t, \b, \n, \r, ', ", \, and $.


Strings are used to hold sequences of characters. Kotlin strings are immutable like in most programming languages.
You can use double quotes or tripple quotes to create String literals. The difference is that Strings with double quotes can be escaped while those with tripple create raw strings.

//new line escaping
val string = "string with \n new line"

//raw string with tripple quotes
val rawString = """
raw string is super useful for strings that span many lines """

In Kotlin strings can also be iterated through.