Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Kotlin offers five categories of basic types. These are:

  • Numbers
  • Booleans
  • Characters
  • Strings
  • Arrays

Numbers

Kotlin offers a set of built-in types to represent numbers.

Booleans

The Boolean type in Kotlin represents objects that can hold a true or false value.

val isTrue: Boolean = true

val isFalse: Boolean = false
​

println(isTrue || isFalse) // prints true

println(isTrue && isFalse) // prints false

println(!isTrue) // prints false

Characters

A single character is represented by the Char type in Kotlin. Character literals always go inside single quotes: ‘Z’

Strings

A sequence of characters is called a string. In Kotlin, these are represented using the String type. String values are enclosed between double quotes: "Hello world"

val message = "Hello World!"

// Creates and prints a new String object
println(message.uppercase())
// HELLO WORLD!

// The original string remains the same
println(message) 
// Hello World!

Arrays

Arrays in Kotlin are a data structure that allows you to store multiple values of the same type. They’re always mutable, and their size is defined at creation time.

var pokemonArray = arrayOf("Pikachu", "Squirtle", "Bulbasaur")

println(pokemonArray.joinToString())

// Pikachu, Squirtle, Bulbasaur

Looking at Nullability

Null safety is one of Kotlin’s marquee features. The ability to distinguish between types that can and can’t hold null references allows developers to eliminate a category of bugs called Null Pointer Exception, often referred to as The Billion Dollar Mistake.

var message: String = "Hello"
message = null // not allowed

var nullableMessage: String? = "Hello"
nullableMessage = null
println(nullableMessage) // prints null

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo