# Topics

• Running Kotlin
• scripting
• compilation
• Terminology
• Anatomy of a Kotlin source file
• Command line arguments
• Program errors (bugs)
• Input/Output

# Running Kotlin

• There are 3 ways to run Kotlin:
1. interactively through the Read Eval Print Loop (REPL)
2. as a scripting language
3. as a compiled language

• To interact with the Kotlin system, type “kotlinc” at the command prompt.
• The Kotlin system should print out a greeting message and the prompt “>>> ”

# 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”
2. Compile the source file by typing

$kotlinc file.kt This produces a class file “FileKt.class”. 3. Run the class file by typing $ 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

$kotlinc -include-runtime -d FileKt.jar file.kt This will produce a jar file named “FileKt.jar” which can be executed by the command $ java -jar FileKt.jar

# Terms

• 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 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 in the previous slide, the ‘kotlin’ program sets up some environment variables, then delegates the interpretation of the commands in the file ‘FileKt.class’ to ‘java’.

# Anatomy of a Kotlin source file

• Here is the standard first Kotlin program.

/* 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 multi-line 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.

/*
*  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.

# 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

"Ho" * 3
thinking it would work like Python.
• 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
2. Interactively by the user typing at the keyboard
• 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.