Misc. Flashcards
How does Modulo % work?
How many times does the RIGHT side go into the LEFT side, then the remainder is the result.
or,
Left side DIVIDED BY Right side, return the remainder.
String Raw multiline?
Use triple quotes... E.g. """ Hello, My name is James. How are you? """
&& AND Short-circuiting
Do the most complex boolean check last because if the first check fails then the last one will not compute.
E.g.
if (quickCheck && complexLongCheck) {
// Do stuff
}
|| OR Short-circuiting
Do the most complex boolean check last because if the first check returns true then the last one will not compute. E.g. if (quickCheck && complexLongCheck) { // Do stuff }
Structural Equality check
==
and
!=
e.g.
var name 1 = “James”
var name 2 = “Dougie”
println(name1 == name2) // Prints false
Referential Equality check
===
and
!===
e.g.
var person1 = Person("jim") var person2 = Person("jim")
println(person1 === person2) // Prints false
What are the terms for the two types of equality in Kotlin?
Structural and Referential
Why don’t referential checks work for ‘primitives’?
In Kotlin traditional primitives (numbers, chars, bools) are converted from Objects to actual primitives at runtime. So you can use the Structural equality check ‘==’ for those.
Strings use the String pool and so have their own logic for equality.
All other ‘custom’ objects work with the referential equality check ‘===’.
What will be the value of strLength after the ‘Nullable Safe’ call if A. myString is null and B. myString is not null.
val strLength = myString?.length
A. null
B. The length of myString.
What does the ‘Elvis’ operator do? ( ?: )
If the anything on the left-hand side of the operator is null, then return the value on the right-hand side.
It's shorthand for... if(person != null) { return person.name } else { return "Anon" // Some default value }
Useful when used with Nullable Safe calls…
return person?age ?: “Anon”
Unit vs Void vs Nothing
TODO
What does invoke() do?
TODO
Can you define functions within a function? From WHEN and WHERE can you call this function?
Yes.
You can only call it after you’ve defined the function,
The ‘inner’ function is scoped to the function it’s defined in so you can’t call it from outside the function.
Can you store a function in a variable?
Yes!
When to use a Single Line Expression?
E.g. fun myFunc = println(“hey”)
Whenever the body of the function can be written on a single line WITHOUT going over the line character limit.
What is a Trailing Lambda and when can it be used?
When a function’s last parameter is a function then when calling that function you can pass the final function argument after the parentheses.
E.g.
someFunction(1stArg, 2ndArg) { println(“3rdArg”) }
If the only parameter a function has is a function then you can omit the parentheses entirely.
E.g.
someFunction { println(“I’m the only Arg”)
How does you define a var args parameter?
With the 'vararg' keyword. E.g. fun myFunction(vararg authors: String)
What can you do in Kotlin to avoid method overloading?
Use default arguments instead.