DT » Programming » Kotlin » Snippets
These examples show some of the key commands and syntax of the Kotlin language.
See the Kotlin language documentation for information about the full language.
| Area | Notes |
|---|---|
| Console Output | Showing messages on the console |
| Console Input | Getting user input from the console |
| Numbers | Defining and using numbers |
| Booleans | Using true / false values |
| Strings | Creating and working with text |
| Characters | Using individual characters |
| Escaped Characters | Using special characters |
| Variables | Creating and using variables to store data |
| Collections | Creating and using lists/sets of data |
| Arithmetic Operators | Mathmatical operations such as adding |
| Boolean/Logical Operators | Logical operations such as AND, OR |
| Branching / Decisions | Allowing a program to make choices |
| Loops / Iteration | Repeating sections of a program |
Numeric data types can be used to store both integer and real (fractional) values. By default, whole numbers are type Int and real/decimal numbers are type Double, however other types are available: Byte, Long and Float are common.
See the Kotlin docs for the full list and details of what values can be held by each type.
val age = 18 // Int by default
val cost = 12345 // Int by default
val profit = 123456789000 // Long due to large value
val height = 1.85 // Double by default
Numeric types can be explicit:
val age: Byte = 18 // Byte due to given type
val cost: Long = 12345 // Long due to given type
val cost = 12345L // Long due to 'L'
val height = 1.85f // Float due to 'f'
Other ways of specifying values:
val profit = 123_456_789_000 // Underscores for readability
val distance = 1.2345e10 // Scientific notation
val code = 0x0FFF // Hexadecimal value via '0x...'
val code = 0b01101110 // Binary value via '0b...'
Numbers can be manipulated using arithmetic operators:
val cost = 1234
val taxRate = 0.175
val finalCost = cost + (cost * taxRate)
Boolean data types can be just one of two values: true or false.
var isUser = true
var isLoggedIn = true
var isManager = true
var isAdmin = false
Booleans can be combined using boolean operators:
if (isUser && isLoggedIn) {
if (isManager || isAdmin) {
...
}
}
Strings are collections of characters, defined using double quotation marks: "Hello!".
See the Kotlin docs for full details on how strings can be used and manipulated.
val name = "Jimmy Pickles"
val phone = "022 123 456"
val message = "Hello, World!"
Strings can contain escaped characters:
val info = "Line one\nLineTwo\nLineThree"
Strings can be concatenated (joined) using the + operator:
val forename = "Jimmy"
val surname = "Pickles"
val fullName = forename + " " + surname // Jimmy Pickles
Variables can be included within strings using string templates:
val forename = "Jimmy"
val surname = "Pickles"
val greeting = "Hello, $forename $surname!" // Hello, Jimmy Pickles!
Characters are individually defined using single quotation marks: 'A'
val initial = 'J'
val prompt = '>'
val digit = '9'
Escaped characters are defined with a leading slash \ and are used within strings where the literal value can’t be used: \n (new line), \' (single quote), \" (double quote), \\ (backslash) and \$ (dollar sign). The full list is here.
println("I shouted \"Hello!\" to her.\nThen I gave her a gift of \$200.")
would result in:
I shouted "Hello!" to her.
Then I gave her a gift of $200.
Unicode characters can be used via hex escape sequences:
println("Here is a kitty: \u0x1f63a")
would result in
Here is a kitty: 😺