**Introduction to Kotlin** # Running Kotlin - There are 3 ways to run Kotlin: 1. interactively through the Read-Eval-Print-Loop (REPL) 1. as a scripting language 1. as a compiled language # Read-Eval-Print-Loop (REPL) - To interact with the Kotlin system, type `kotlinc` at the command prompt. - The Kotlin system should print out a greeting message and the prompt ">>> " like the following ```bash $ kotlinc Welcome to Kotlin version 1.9.10 (JRE 20.0.2) Type :help for help, :quit for quit >>> ``` - You can now type at the prompt an expression or a statement. - If you type in an expression, the system evaluates the expression. If it is valid, it prints out the value of the expression. Otherwise, it prints out an error message. It then prints out the prompt. - If you type in a statement, the system will try to execute it. If the statement is valid, the system executes the statement and possibly prints something out as a side-effect. If the statement is invalid, the system prints out an error message. It then prints out the prompt. # Scripting - A _script_ is a text file containing _commands_ to the Kotlin language processor. - A Kotlin script must end in the file extension `.kts` - Statements in the script can also be expressions, but unlike in REPL mode the values of the expressions won't be printed out automatically. If you want to see the values, you have to call the `print()` or `println()` function. - Steps to create and run a script: + use a text editor to edit the script, say named `file.kts` + run the script by typing ```bash $ kotlinc -script file.kts ``` # Compilation - Here are the classic steps to develop a Kotlin compiled program: 1. Use a text editor to edit a _source file_, say named `file.kt` 1. Compile the source file by typing ```bash $ kotlinc file.kt ``` This produces a class file `FileKt.class`. 1. Run the class file by typing ```bash $ kotlin FileKt ``` - If the compile step (Step 2) or run step (Step 3) above produces an error, you'll have to go back to the edit step (Step 1) to fix the problem. This edit-compile-run loop is repeated as many times as necessary. - For deployment, you can compile the source to a ".jar" file by typing ```bash $ kotlinc -include-runtime -d FileKt.jar file.kt ``` This will produce a jar file named "FileKt.jar" which can be executed by the command ```bash $ java -jar FileKt.jar ``` Unfortunately, this method works only if you're using only the builtin kotlin library & java library. If you're using any outside library, you'll have to give `kotlinc` a lot more information. # Terminology - The name of a _source file_ must end in the `.kt` file extension. It is usually known as the "Kotlin program". It contains your instructions written in the Kotlin programming language. - Kotlin programs have to be compiled, i.e., translated, to a language for a fictitious machine named the _Java Virtual Machine_ (JVM). The result of the translation is written to a _class file_. - The name of a class file ends in the `.class` file extension. It contains the _bytecode_ instructions, i.e., strings of 0's and 1's that are instructions for the JVM, and are equivalent in meaning to the Kotlin source program. - A _compiler_ translates a program written in a high-level language into an equivalent _machine language_ program. - The compiler that translates a Kotlin program into the JVM machine language is named **kotlinc**. - A JVM is usually implemented in software. Though rarely done, it can also be implemented in hardware. - An _interpreter_ takes a program in the JVM machine language and executes it on a real processor. - The interpreter that executes a JVM language program on your processor is named **java**. When you execute the command `kotlin FileKt` as described previously, the `kotlin` program sets up some environment variables, then delegates the interpretation of the commands in the file `FileKt.class` to the `java` interpreter. # Anatomy of a Kotlin source file - Here is the standard first Kotlin program. ```kotlin /* Prints "Hello, World" to standard output. */ fun main() { // This is line comment println("Hello, World") } ``` - Each Kotlin _statement_ may optionally be terminated by a semicolon. - In contrast to _python_ programs where indentation matters semantically, a _Kotlin_ program is "free format" except that each statement must end in a newline. - A _line comment_ starts with `//`. The double slash and everything that follows till the end of the line are ignored by the compiler. - A _multiline comment_ can span more than one line. It starts with `/*` and ends with `*/` and can nest. # Command line arguments - This program shows how to process command line arguments. ```kotlin /* * Prints "Hi, Bob. How are you?" where "Bob" is replaced by the * command-line argument. */ fun main(args: Array< String >) { print("Hi, ") print(args[0]) println(". How are you?") } ``` - Program starts execution with an automatic call to the `main` function. - The parameter `args` is an array of strings corresponding to the command line arguments typed in by the user when they invoke the program. The name `args` is simply a convention. You can name it anything you want as long as it obeys the syntax of a kotlin variable name, but most people just follow convention. # Program errors (bugs) - A mistake in your Kotlin program, called a _program error_ (or _bug_), + disrupts the normal progress in the classic program development steps, or + causes an early or unexpected termination of program execution, or + causes an incorrect behavior during program execution. - A error can occur during + _compile time_ + _run time_ - A special kind of compile-time error is called _syntax error_. It occurs when your Kotlin program violates some rules of how a Kotlin program should be written. Another kind of compile-time error is called _type error_. It occurs when a program statement violates some rules of type usage. - A special kind of run-time error is called _logic error_. It is caused by a mistake in the algorithm and is usually the nastiest kind of bug. # Error examples - Examples of syntax errors: + using the wrong letter case for a keyword, e.g., writing 'While' instead of 'while' + unmatched parenthesis, braces, etc. - Example of type error: + writing ```kotlin "Ho" * 3 ``` thinking it would work like Python. To get what you want, you have to say ```kotlin "Ho".repeat(3) ``` instead. - Examples of run-time errors: + failing to provide a command line argument when the program expects one + calling 'print()' when a 'println()' is needed # Input/Output - An executing program can get its input in two ways. 1. Through the command line arguments 1. Interactively by the user typing at the keyboard, clicking the mouse, etc. - An executing program normally writes its output to the computer screen by calling the `print()`, or `println()` function. - Both input and output are in the form of _text streams_. _---San Skulrattanakulchai_