GNU/Linux Programmer's Solver - Kotlin

Hello World program in Kotlin. Basic structure, comments and string literals
PROGRAMMING KOTLIN HELLO WORLD
/* Multiline
   Comment */
fun main () {
    println ("Hola MUndo!"); // This is one line comment
    println ("""This is 
a text string in multiple lines
like in Python""");
}

Kotlin is a new programming language focused on Android application development as an alternative to Java. The language generates bytecode that runs on the Java Virtual Machine, so once compiled, a Kotlin program can run on any system that can execute Java bytecode.

Like with Java and other languages we’ve seen, Kotlin programs start their execution in a function called main, but unlike Java, it’s not necessary to define a class to be able to write our main function, which makes the program a little shorter.

Comments follow the standard imposed by C/C++ and, as we can see, the language supports multi-line strings by defining them using triple quotes, as in Python. Like in many of the languages we’ve already seen, Kotlin uses the ; character to mark the end of instructions, although in Kotlin it’s optional and can be omitted, only being necessary in cases where it’s not possible to unambiguously deduce the end of an instruction. The curly braces {} are used to group instructions into blocks.

Like in Java, Kotlin uses the suffix ln to indicate whether we want to add a carriage return to the string we want to print or not.

Kotlin programs use the .kt extension.

The official website contains instructions on how to install the compiler, which is offered in two varieties: the general compiler that generates Java bytecode and a native version capable of generating native code. Both can be downloaded from the project’s Github.

Using the compiler, we can generate our program with a command like the following:

$ kotlinc hello.kt
$ java HelloKt

If we don’t provide extra parameters, the compiler generates a name for the output .class file based on the input file name.

We can also generate .jar files using the -d flag:

$ kotlinc hello.kt -d hello.jar
$ java -jar hello.jar

konanc and kotlinc-native are scripts that execute the same command. If you don’t specify a name with the -o flag, the compiler will generate a program called program.exe.

The environment allows you to generate binaries for different platforms:

$ konanc -list-targets
linux_x64 (default)
linux_arm32_hfp (deprecated)
linux_arm64
mingw_x64
android_x86
android_x64
android_arm32
android_arm64

$ konanc -target linux_arm64 -o hello.arm hello.kt
$ ./hello.arm.kexe

DID YOU KNOW…

Kotlin programs can be compiled to Java bytecode, Native Code for different platforms, JavaScript, or even Web Assembly, although the latter option is experimental.

Kotlin was announced in 2011 by the company JetBrains, known for developing the IntelliJ IDEA IDE for Java. The name of the language is taken from Kotlin Island in Saint Petersburg, apparently as a nod to Java, which also takes its name from an island. The language uses the Java Virtual Machine (JVM).

Version 1.0 was released in 2016, and subsequent versions have added new capabilities to the language. In 2019, Google announced that Kotlin is becoming the language of choice for Android app development.

Summary

  • Interpreted language and compilable to bytecode and native code
  • Extension: kt
  • Line terminator: ; (optional)
  • Groups instructions with: { instructions }
  • Multi-line comments: /* comment */
  • Same-line comments: //
  • String delimiter: "string"
  • You can print text with println (Does not add a carriage return)
  • Support for multi-line strings: """ multi-line string """
  • Entry point: main function

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Java

Hello World program in Java. Basic structure, comments and string literals

Read

PROGRAMMING JAVA HELLO WORLD

GNU/Linux Programmer's Solver - PHP

Hello World program in PHP. Basic structure, comments and string literals

Read

PROGRAMMING PHP HELLO WORLD

Return to Home Page