Instruction
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.
For integer numbers, these are as follows:
- Byte - 8 bits in size. Can hold values between -128 and 127.
- Short - 16 bits in size. Can hold values between -32768 and 32767.
- Int - 32 bits in size. Can hold values between -2,147,483,648 and 2,147,483,647.
- Long - 64 bits in size. Can hold values between (-2^63 ) and (2^63 -1).
For real numbers, Kotlin provides floating point types as follows.
- Float - 32 bits in size. Offers 24 significant bits and 8 exponent bits
- Double - 64 bits in size Offers 53 significant bits and 11 exponent bits.
Booleans
The Boolean type in Kotlin represents objects that can hold a true or false value.
Boolean types offer a few built-in operations, shown below:
-
||– disjunction (logical OR) -
&&– conjunction (logical AND) -
!– negation (logical NOT)
Here’s an example of the operators above:
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’
Kotlin also supports several special characters( i.e., escape sequence) to format the console output. These are:
-
\t– tab -
\b– backspace -
\n– new line (LF) -
\r– carriage return (CR) -
\'– single quotation mark -
\"– double quotation mark -
\\– backslash -
\$– dollar sign
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"
It’s worth noting that all String values are immutable, meaning their values can’t change once assigned. Any transformation applied to a string in Kotlin is returned as a new String object, leaving the original value intact.
Here’s an example demonstrating the same:
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.
Here’s an example demonstrating arrays:
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.
Kotlin handles null types by appending the type signature with a ?. This means a regular string in Kotlin can’t hold null. To do so, the type must be changed from String to String?, which is a nullable variant of String.
Here’s an example demonstrating null types in Kotlin:
var message: String = "Hello"
message = null // not allowed
var nullableMessage: String? = "Hello"
nullableMessage = null
println(nullableMessage) // prints null