Hello World in Kotlin

Hello World in Kotlin Featured Image

With the current trend of new language articles in the Hello World in Every Language series, I figured we might as well cover one of the more popular ones. In this article, we’ll be tackling Hello World in Kotlin, a language which first debuted in 2011.

Table of Contents

Kotlin Background

Since Kotlin is a bit more popular than most of the newer languages, it actually has a Wikipedia page. So, we’ll use that to learn more.

According to Wikipedia, Kotlin is a programming language that runs on the Java Virtual Machine. In other words, Kotlin compiles down to Java bytecode. In fact, developers have the option to decide which version of Java bytecode they want. In addition to the JVM, Kotlin can also compile down to JavaScript.

In terms of features, Kotlin offers an aggressive form of type inference. In other words, the language supports static type checking on implicit types. Of course, the benefit is a far less verbose syntax than Java.

Of course, I think my favorite feature is extension methods. In Kotlin, we can take a class that already exists and tack on our own methods without creating an extension class. For instance, Wikipedia shares the following snippet:

fun String.lastChar():  Char = this.get(this.length - 1)

In this example, the lastChar method is added to the String class. How cool is that?

Hello World in Kotlin

Anyway, let’s get down to business—Hello World in Kotlin:

package hello

fun main(args: Array<String>) {
  println("Hello, World!")

Now, time to dig in!

On the first line, we have the package declaration. Like most languages, this basically declares the package or module name of this file. If anyone needed to use a function in this file, they could access it via the package name.

Next, we have the function definition. In this first line, we can see we define the main function which receives an array of Strings as input. In a lot of languages, types are declared in type-var order, not in Kotlin. In Kotlin, we declare the variable name before giving it a type.

Finally, we print Hello World in Kotlin. Like many languages, we use a simple call to the println function, so no surprises there.

How to Run the Solution

At this point, we probably want to actually run the Hello World in Kotlin code snippet. Perhaps the easiest way to do so is to leverage the online Kotlin editor.

Alternatively, we can use the latest standalone compiler. Of course, we’ll want to get a copy of Hello World in Kotlin while we’re at it. With both in hand, all we need to do is navigate to the folder containing our files and run the following:

kotlinc hello-world.kt -include-runtime -d hello-world.jar
java -jar hello-world.jar

Apparently, the standalone Kotlin compiler compiles Kotlin down to a runnable jar which we can then execute using the Java Runtime Environment. Of course, I haven’t tried it. After all, I pulled these directions from Kotlin documentation. Let me know if this works in the comments.

Sample Programs in Every Language

Well, that’s it! Thanks again for stopping by.

Up next, I’d like to keep the new language trend going. In fact, I’m interesting in sharing some information on Pyret, a fun little educational language which first appeared in 2012.

At any rate, if you enjoyed this article, don’t forget to share it. Also, feel free to give your recommendations for future articles in the comments.

Series Navigation← Hello World in CrystalHello World in Visual Basic .NET →

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.