Skip to content

Latest commit

 

History

History

learn-kotlin

Learn Kotlin Tutorial

Kotlin project Directory learn-kotlin\ gathers Kotlin code examples coming from the Learn Kotlin tutorial [1] (GitHub repository).
It also includes several batch scripts for experimenting with Kotlin on a Windows machine.

🔎 The following Kotlin code examples, originally written by Steven Perry in 2018, have been updated to Kotlin 1.6 and formatted with Ktlint.

> tree /a /f | findstr /v /b [A-Z]
|   build.bat
|   build.gradle
|   build.sh
|   build.xml
|   gradle.properties
|   Makefile
|   pom.xml
\---src
    \---main
        \---kotlin
                Example1.kt
                Example2.kt
> build -verbose clean run
Compile 2 Kotlin source files to directory "target\classes"
Execute Kotlin main class "com.makotojava.learn.kotlin.example2.Example2Kt"
Greetings from example 2

This tutorial part presents the Kotlin primitive types and literals; it has the following directory structure :

> tree /a /f | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.gradle
|   build.sh
|   build.xml
|   gradle.properties
|   Makefile
|   pom.xml
\---src
    +---main
    |   \---kotlin
    |           BooleanExample.kt
    |           ByteExample.kt
    |           CharExample.kt
    |           DoubleExample.kt
    |           FloatExample.kt
    |           IntExample.kt
    |           LongExample.kt
    |           Primitives.kt
    |           ShortExample.kt
    \---test
        \---kotlin
                PrimitivesTest.kt

For instance, command build clean run executes all examples while command build clean run:Double executes only example DoubleExample.kt:

> build clean run:Double
The value of doubleMin is: 4.9E-324
The value of doubleMax is: 1.7976931348623157E308
The value of double(1000000000000.0001) is: 1.0000000000000001E12
The value of double(1.0e22) is: 1.0E22
The value of double(-Double.MAX_VALUE) is: -1.7976931348623157E308

This tutorial part presents Kotlin binary operators and basic functions

> build
Usage: build { <option> | <subcommand> }

  Options:
    -debug            print commands executed by this script
    -script           execute the Kotlin script
    -timer            print total execution time
    -verbose          print progress messages

  Subcommands:
    clean             delete generated files
    compile[:<name>]  generate class files
    help              print this help message
    lint[:<name>]     analyze Kotlin source files and flag programming/stylistic errors
    run[:<name>]      execute the generated program
  Valid names are: all (default), examples

For instance, command build -script -verbose run executes the script file Examples.kts instead of the source file Examples.kt:

> build -script -verbose run
Execute Kotlin script file "src\Examples.kts"
byte10 = 10

This tutorial part introduces Kotlin classes; it has the following directory structure :

> tree /a /f | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.gradle
|   build.sh
|   build.xml
|   gradle.properties
|   Makefile
|   pom.xml
\---src
    \---main
        \---kotlin
            +---example1
            |       Person.kt
            +---example2
            |       Person.kt
            +---example3
            |       Person.kt
            \---example4
                    Family.kt
                    Neighbor.kt
> build -verbose clean run
Compile 5 Kotlin source files to directory "target\classes"
Execute Kotlin main class "com.makotogo.learn.kotlin.example2.PersonKt"
Person: Family Name=Neumann, Given Name=Susan, Date of Birth=1980-03-17

This tutorial part presents more advanced Kotlin features, such as default arguments, function types, names arguments and nullable variables i.e. Default.kt, FunctionTypes.kt, Named.kt and Nullable.kt.

> build
Usage: build { <option> | <subcommand> }

  Options:
    -debug            print commands executed by this script
    -timer            print total execution time
    -verbose          print progress messages

  Subcommands:
    clean             delete generated files
    compile[:<name>]  generate class files
    help              print this help message
    lint[:<name>]     analyze Kotlin source files and flag programming/stylistic errors
    run[:<name>]      execute the generated program
  Valid names are: defaultargs (default), functiontypes, namedargs, nullable

Unit 8

Source file Strings.kt presents the usage of string literals, string templates and string operations.

Unit 9

Source file Equality.kt presents the differences betwen stuctural and reference equality; other files present their usage with comparison operators (Comparison.kt) and conditional constructs (If.kt, When.kt).

Unit 10

This unit presents iterative constructs; it has the following directory structure :

> tree /a /f | findstr /v /b [A-Z]
|   build.bat
|   build.gradle
|   build.xml
|   gradle.properties
|   Makefile
|   pom.xml
\---src
    \---main
        \---kotlin
                Arrays.kt
                For.kt
                Main.kt
                Ranges.kt
                While.kt

Footnotes

[1] Learn Kotlin Tutorial

The 7 first units from Steven Perry's tutorial (GitHub repository) are available from IBM Developer network :

  • Unit 1 : Overview of the Kotlin learning path.
  • Unit 2 : Set up your Kotlin development environment.
  • Unit 3 : Object- and function-oriented programming concepts and principles.
  • Unit 4 : Get started with Kotlin.
  • Unit 5 : More Kotlin basics.
  • Unit 6 : Your first Kotlin class.
  • Unit 7 : All about functions.

mics/October 2024