Hello World in Kotlin

Published on 03 April 2019 (Updated: 03 April 2019)

Hello World in Kotlin

In this article, we’ll be covering Hello World in Kotlin.

How to Implement the Solution

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.1

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.1


References

  1. J. Grifski, “Hello World in Kotlin,” The Renegade Coder, 29-Apr-2018. [Online]. Available: https://therenegadecoder.com/code/hello-world-in-kotlin/. [Accessed: 03-Apr-2019].  2