Skip to content

Commit 4e724bd

Browse files
committed
Move Lesson #2: Basic Types and Exercise to Documentation
In this commit, the data related to Lesson #2: Basic Types and the exercise has been removed from the current file and relocated to the documentation. The content has been organized into separate folders, 'lessons' and 'exercises', for better structuring and clarity. This ensures that the main file remains clean and concise, while the detailed information can be found in the respective documentation folders. This modification enhances the maintainability and organization of the project.'
1 parent 5c16a98 commit 4e724bd

File tree

3 files changed

+90
-34
lines changed

3 files changed

+90
-34
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Exercise: Variable declaration
2+
3+
## Instructions
4+
5+
Explicitly declare the correct type for each variable:
6+
7+
```kotlin
8+
fun main() {
9+
val a = 1000
10+
val b = "log message"
11+
val c = 3.14
12+
val d = 100_000_000_000_000
13+
val e = false
14+
val f = '\n'
15+
}
16+
```
17+
## Solution
18+
```kotlin
19+
fun main() {
20+
val a: Int = 1000
21+
val b: String = "log message"
22+
val c: Double = 3.14
23+
val d: Long = 100_000_000_000
24+
val e: Boolean = false
25+
val f: Char = '\n'
26+
}
27+
```
28+
The code represents the declaration of variables with explicitly specified types and assignment of corresponding values. Here's an explanation of the code execution:
29+
30+
- Variable **a** is declared with the type **Int** and assigned the value **1000**.
31+
- Variable **b** is declared with the type **String** and assigned the value "**log message**".
32+
- Variable **c** is declared with the type **Double** and assigned the value **3.14**.
33+
- Variable **d** is declared with the type **Long** and assigned the value **100_000_000_000**. Note that the underscore character is used in the number for improved readability.
34+
- Variable **e** is declared with the type **Boolean** and assigned the value false.
35+
- Variable **f** is declared with the type **Char** and assigned the value '**\n**', which represents the newline character.
36+
As a result of executing this code, all variables will be declared with explicitly specified types and their corresponding values. This provides the compiler with information about the data types that can be used and supported for each variable.
37+
38+
Good luck!
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Lesson 2: Basic Types and Variable Declarations
2+
In Kotlin, every variable and data structure has a data type, which is important for determining the operations that can be performed on them. Kotlin supports type inference, where the compiler can automatically infer the data type based on the assigned value.
3+
4+
## Basic Types
5+
Kotlin provides several basic types for different kinds of data:
6+
7+
- Integers: **Byte**, **Short**, **Int**, **Long**
8+
- Unsigned Integers: **UByte**, **UShort**, **UInt**, **ULong**
9+
- Floating-point Numbers: **Float**, **Double**
10+
- Booleans: **Boolean**
11+
- Characters: **Char**
12+
- Strings: **String**
13+
For more detailed information about basic types and their properties, refer to the [Kotlin documentation on Basic Types](https://kotlinlang.org/docs/basic-types.html).
14+
15+
## Variable Declarations
16+
Variables can be declared and initialized in Kotlin. If a variable is declared without initialization, its type must be specified using the **:** operator.
17+
18+
```kotlin
19+
// Variable declared without initialization
20+
val d: Int
21+
// Variable initialized
22+
d = 3
23+
24+
// Variable explicitly typed and initialized
25+
val e: String = "hello"
26+
```
27+
Variables must be initialized before the first read, but Kotlin allows for initialization at a later stage.
28+
29+
## Augmented Assignment Operators
30+
Kotlin provides augmented assignment operators (**+=**, **-=**, ***=**, **/=**, **%=**) for performing arithmetic operations and updating the variable in-place.
31+
32+
```kotlin
33+
var players = 5
34+
35+
// Some players leave the game
36+
players = 3
37+
38+
players = players + 1 // Example of addition: 4
39+
players += 5 // Example of addition: 9
40+
players -= 2 // Example of subtraction: 7
41+
players *= 3 // Example of multiplication: 21
42+
players /= 7 // Example of division: 3
43+
```
44+
## Variable Usage
45+
Variables can be used in string interpolation using the **$** symbol:
46+
47+
```kotlin
48+
println("Number of players: $players") // Number of players: 3
49+
println("Integer: $woInit") // Integer: 7620
50+
println("My name is $myNameIs") // My name is Artem Zarubin
51+
```
52+
Now you have a basic understanding of basic types, variable declarations, and augmented assignment operators in Kotlin!

src/main/kotlin/Main.kt

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,4 @@
11
fun main(args: Array<String>) {
2-
// Lesson #2. Basic types
3-
var players = 5
4-
5-
// Some players leave the game
6-
players = 3
7-
8-
players = players + 1 // Example of addition: 4
9-
players += 5 // Example of addition: 9
10-
players -= 2 // Example of subtraction: 7
11-
players *= 3 // Example of multiplication: 21
12-
players /= 7 // Example of division: 3
13-
14-
println("Number of players: $players") // Number of players: 3
15-
16-
// Variable declared without initialization
17-
val woInit: Int
18-
// Variable initialized
19-
woInit = 7620 // My favorite number
20-
21-
// Variable explicitly typed and initialized
22-
val myNameIs: String = "Artem Zarubin"
23-
24-
// Variables can be read because they have been initialized
25-
println("Integer: $woInit") // Integer: 7620
26-
println("My name is $myNameIs") // My name is Artem Zarubin
27-
28-
val aa: Int = 1000 // Exercise #2. Explicitly declare the correct type for each variable.
29-
val bb: String = "log message"
30-
val cc: Double = 3.14
31-
val dd: Long = 100_000_000_000_000
32-
val ee: Boolean = false
33-
val ff: Char = '\n'
34-
println()
35-
362
// Lesson #3. Collections
373
// Read only list
384
val readOnlyShapes = listOf("Circle", "Triangle", "Square")

0 commit comments

Comments
 (0)