Key Concepts Flashcards

Summary key/unique Kotlin concepts

1
Q

Ranges

A

An interval that has a:

  • start value
  • end value

Common options for defining an interval:

  • step(n)
  • reversed()

val aToZ = “a”..”z”
val containsC = “c” in aToZ
1..9
val countDown = 100.downTo(0)
val rangeTo = 1.rangeTo(30)
val oddNumbers = rangeTo.step(2)
val countDownEvenNumbers s = (2..100).step(2).reversed()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

String Templates

A

Embed values, variables, or expressions inside a string
without pattern replacement or string concatendation

—————————————————————————————–

“Hello $firstName”

“Your name has ${name.length} characters”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Loops

A

while(true)

for (k in list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Equality

A

referential

  • structural
  • ————————————–*

val sameReference = a === b

val structuralEquality = a == b
# == is null safe
# calls .equals() on each object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Current Receiver

A

The object reference referred to
by the this keyword
—————————————————

  • object to the left of the ​dot notation
  • object of the class in which this function is defined
  • OuterInstanceClass.this from nested classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

visibility modifiers

A

private
C only be accessed from the same file

protected
Visible only to other members of the class
or interface, including subclasses.
Cannot include top-level functions, classes or interfaces.

internal
Visible only from other classes, interfaces and functions
within the same IntelliJ, Maven or Gradle module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

control flow as expressions

A

The following control flow blocks are expressions,
versus statements:

  • if…else
  • try…catch
fun isTuesday() {
 // must include the else clause
 if (LocalDate.now().getDayOfWeek() == "TUESDAY") true else false
}

val success = try {
readFile()
true
} catch (e: IOException) {
false
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

null syntax

A

val firstName: String? // allow null value
val firstsName: String = null // compile time error

val firstName: String? = anyThing is String // blows up if anyThing is null
val firstName: String? = anyThing is? String // safe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

type checking
and casting

A

is
is?
smart casts
explicit casts
————————-

anyThing is String // will throw a runtime exception if anyThing is null

if (anyThing is String) {
println(anyThing.length)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

when

A
  • asdfasdf
  • —————————————————-*
  • only exhaustive when used as an expression
  • what is the danger of the else clause ?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly