Lesson 1 - Lab 1.2
Lesson 1 - Lab 1.2
1
2
1.2. Open and execute code in Kotlin's REPL (Read-Eval-Print Loop) in IntelliJ
IDEA
3
2. Learn about operators and types
2.1. Explore numeric operators
- Enter the following expressions in the REP then see the result
4
2.2. Practice using types
- Define a variable of type Int in the REPL.
- Create a new variable, then enter the variable name shown above, followed
by .to
5
- Assign a Byte value to variables of different types.
- For the assignments that returned errors, try casting them instead
6
- Define some variables and specify the type explicitly.
7
3. Compare conditions and booleans
- Write an if/else statement
8
4. Learn about nullability
4.1. Learn about nullability
- Declare an Int and assign null to it.
- Use the question mark operator, ?, after the type to indicate that a variable can
be null. Declare an Int? and assign null to it.
9
- A point about null pointers
10
5. Explore arrays, lists, and loops
5.1. Make lists
- Declare a list using listOf and print it out
- An array declared with arrayOf doesn't have a type associated with the
elements, so you can mix types, which is helpful. Declare an array with different
types.
- You can also declare arrays with one type for all the elements. Declare an array
of integers using intArrayOf()
11
- Try out different combinations of nested arrays and lists.
- One nice feature of Kotlin is that you can initialize arrays with code instead of
initializing them to 0
- You can loop through the elements and the indexes at the same time
12
- Kotlin has while loops, do...while loops, and ++ and -- operators. Kotlin also has
repeat loops.
13
6. Summary
Kotlin is very similar to other languages when it comes to the basics like
operators, lists, and loops, but there are some important differences.
The following features may be different in Kotlin than what you're used to in
other languages:
• Kotlin types can't be implicitly converted—use casting.
• Variables declared with val can only be assigned once.
• Kotlin variables are not nullable by default. Use ? to make variables nullable.
• With Kotlin, you can loop through the index and elements of an array at the
same time in a for loop.
The following Kotlin programming constructs are similar to those in other
languages:
• Arrays and lists can have a single type or mixed types.
• Arrays and lists can be nested.
• You can create loops with for, while, do/while, and repeat.
• The when statement is Kotlin's version of the switch statement, but when is
more flexible.
14
7. Learn more
• Explicit type conversion
• Defining variables
• String templates
• Nullable values
• Lists
• Arrays
• if, when, for, while
• ?: (Elvis) operator
15