PK - Chapter 2 Flashcards
What are the 2 keywords for
declaring variables ?
val - immutable
var - mutable
Define constant foo = Test
val foo = “Test”
Define variable foo with an explicit type of Number and double value of 12.3
var foo: Number = 12.3
Is referential equality guaranteed
on boxed values ?
No
Even if same value, may have
been boxed in different locations.
What are the built-in number types
and their memory sizes ?
There are 6:
Long - 64
Int - 32
Short - 16
Byte - 8
Double - 64
Float - 32
# Define variable foo of type Long with value = 12,333
var foo = 12_333L
# Define variable foo of type double with value 12.33
var foo = 12.33
# Define variable foo with type of Float and value of 12.33
var foo = 12.33F
Create a number literal for each
built-in Number type
There are 6 build-in Number types:
Long
val foo = 1234L
Int
val foo = 1234
Double
val foo = 12.34
Float
val foo = 12.34F
Hexadecimal
val foo = 0xAB
Binary
val foo = 0b01010101
What is the default type for
loating point numbers ?
Double
What is the default type
for integral numbers ?
Int
Convert an Int to a Long
val fooInt = 1234
val fooLong = fooInt.toLong()
Convert a Float to a Double.
val fooFloat = 12.34F
val fooDouble = fooFloat.toDouble()
What are the boolean operations ?
There are 3:
- negation !
- conjunction &&
- disjunction ||
Bonus:
conjunction and disjunction are lazy
What are the bitwise operators ?
There are 6:
shl - Shift Left
shr - Shift Right
ushr - Unsigned Shift Right
and
or
xor
What are the
Char escape sequences ?
There are 9:
\t
\b
\n
\r
‘
“
$
\u1234
Bonus:
Char type not treated as a number, as in Java.
What is a raw string ?
What is the syntax ?
Typical use ?
- No escaping is necessary, so all characters can be included
- The string starts and ends with 3 double quotes (“””)
val foo = “””
This is the first line
second line of the string
third line with special character / in the string”””
- Typically used for multi-line strings or regex strings
What functions are provided
by an array ?
There are 4:
iterator()
size()
get(index)
set(inces, value)
Bonus:
get() and set() are also available through
bracket syntax.
val element1 = fooArray[0]
What are the primitive array classes ? Why use them ?
There are 8 primitive array classes:
CharArray
BooleanArray
LongArray
IntArray
ShortArray
ByteArray
DoubleArray
FloatArray
Avoids boxing to improve performance.
What are the max values
for built-in Number types ?
They can be retrieved using Type.MAX_VALUE.
Int.MAX_VALUE =
Long.MAX_VALUE =
Short.MAX_VALUE =
Float.MAX_VALUE =
Double.MAX_VALUE =
Create an Int array of 1, 2, 3.
val fooIntArray = arrayOf(1, 2, 3)
How are Kotlin arrays
different from Java arrays ?
Arrays are not part of the language.
Arrays are regular collection classes.
What is the syntax for comments ?
Same as Java.
// line comment
/*
A block comment,
which can span many
lines.
*/
How are packages handled in Kotlin ?
Same as Java.
package com.company.theapp class Foo
How are imports handled in Kotlin ?
Same as Java.
import com.company.theapp.Foo
import com.company.theapp.packagez.*
Give an example of import renaming.
import com.company.theapp.Foo
import com.company/theotherapp.Foo as Foo2
Give an example of string templates.
val name = “Sam”
val nameHello = “Hello $name”
val nameLength = “$nameHello. Your name has ${name.length} characters.”
Bonus:
Also called string interpolation.
Give an example of string interpolation.
val name = “Sam”
val nameHello = “Hello $name”
val nameLength = “$nameHello. Your name has ${name.length} characters.”
Bonus:
Also called string templates.
Explain ranges.
A range is an interval that has a start value
and an end value.
Any types which are comparable can be used
to create a range, using the “..” operator
val aToZ = “a”..”z”
val oneToNine = 1..9
Explain the “in” operator.
The “in” operator is used to test whether a
given value is included in the range.
val aToZ = “a”..”z”
val isTrue = “c” in aToZ