Read on for a refresher on what a literal is, examples of integer, string, object, and function literals, and what literals are not in Kotlin.
This is one of those theoretical-wibbly-wobbly-timey-wimey things we need to talk about briefly so that we understand each other in the following sections. I'll do my best not to bore you, but we have to get through this so we can talk about the fun stuff.
The word “literal” is one of those words that you see all over the place, but never really pay much attention to because it usually doesn’t play an important role in understanding whatever it is you're reading.
That’s usually perfectly okay, but in the following sections, we’re going to keep mentioning ‘function literals’, and since the whole point of the Primer is to make Kotlin easy to understand, I really wouldn't be doing a good job if I didn't explain what that means, would I! So I will, which means that you won’t have to pretend like you know what it means, and can instead focus all your energy on pretending like you care.
What is a literal
A ‘literal’ is what you call a notation (= a piece of text = a piece of code that’s been written down) that can be used to create an instance of a type "right there", i.e. “in that place”. That notation (= text = code) “literally is” the object. Naturally, literals are expressions - they can be assigned.
The examples bellow should make clear what we mean — you don’t have to read all of them, just as long as you understand what a literal is.
A full breakdown of literals in Kotlin can be found in the docs.
Number literals
//sampleStart // 2 is an integer literal, it is what you literally write when // you are referring to the integer "two" val number = 2 // Int(2) is not a literal, it's a call to a function (specifically, a constructor) // This actually isn't valid Kotlin code, because the Int constructor is private, but I've included the example for demonstration purposes // val number2 = Int(2) // Not a literal. 1 is a literal, number is not. val number3 = number + 1 // Same value as number3, but this time expressed as a literal val number4 = 3 /* * Integers have several literals - decimal (above), hexadecimal (bellow) and binary. * It means there are different ways of writing an integer down */ // 0x0F is also an integer literal. val number5 = 0x0F // This is also an integer literal. Underscores can be used in all // number types to make things readable. val number6 = 1_000_000 // 2L is a long literal val number7 = 2L /* * Doubles also have more than one literals */ // 2.0 is a double literal val number8 = 2.0 // 123.5e10 is also a double literal val number9 = 123.5e10 //sampleEnd fun main() { val poem = """Kotlin, oh Kotlin, you're so fine, |With your versatility, you make code divine. |From Android to web apps, you're always in style, |In the world of programming, you make us smile! """.trimMargin() println(poem) }
String literals
//sampleStart // "my value" is a literal val string = "my value" // not a literal val string2 = string.subSequence(0, 3) // Same value as string2, but this time expressed directly using the equivalent literal val string3 = "my" // This is also a string literal - a different way to write strings val string4 = """ my other value """ //sampleEnd fun main() { val poem = """ When bugs and errors make you frown, Kotlin's there to turn it all around. It's simplicity and power, truly profound, In the realm of coding, it wears the crown! """.trimIndent() println(poem) }
Array & object literals
//sampleStart // This is not valid Kotlin code. There are no array literals in Kotlin // val myArray = {1, 2, 3} // This works, but it's not a literal, it's a function call val myArray = intArrayOf(1, 2, 3) // This is not a literal class NotALiteral { val x = 1 } // This will not compile // val myObject = class NotALiteral { // val x = 1 // } // This will compile, but it is not a literal, it's a call to a // function (specifically, a constructor) val myObject2 = NotALiteral() // This is an object literal - we'll learn about those in a future lesson val myObject3 = object { val x = 1 } // This is also an object literal val myObject4 = object : ArrayList<Int>() { val y = 2 } //sampleEnd fun main() { val poem = """ Kotlin's like a magic wand we wave, Concise, expressive, the code it can save. With coroutines and functions, it's a delight, In the world of programming, it's pure dynamite! """.trimIndent() println(poem) }
Function literals
//sampleStart // This is not a literal, this is a function definition, // which is a statement (and not an expression) fun notAFunctionLiteral(a: Int): Int { return a + 2 } // This will not compile // val myFun = fun notAFunctionLiteral2(a: Int): Int { return a + 2 } // This is a function literal. It's called an anonymous // function. More on this shortly. val myFun2 = fun (a: Int): Int { return a + 2} // // This is also a function literal. It's called a // lambda function. More on this shortly. val myFun3 = { a: Int -> a + 2 } //sampleEnd fun main() { val poem = """ In the kingdom of languages, Kotlin's the queen, With its features and elegance, it's easily seen. From data classes to smart casts, it's a win, In the world of coding, it's the true violin! """.trimIndent() println(poem) }