Learn the Basics of the Kotlin Language

May 22 2024 · Kotlin 1.9, Android 14, Kotlin Playground 1.9

Lesson 01: Setup Your Environment


Episode complete

Play next episode


There’re many ways to write and run Kotlin programs. In this demo, you’ll learn how to use each of them.

Kotlin Playground

The first method you’ll try is the Kotlin Playground. It’s probably the quickest way to try out Kotlin. Visit https://play.kotlinlang.org in your browser. It opens a Kotlin editor session where you can begin to write Kotlin code. Click the Run button at the far right corner to execute the code in the editor. The output is displayed in the console below. In the editor, change “Hello, world!!!” to “Hey Kotlin!!!”. Then click the Run button. The output console below displays “Hey Kotlin!!!”.

In the top-left corner, you’ll find the Kotlin version drop-down. This option lets you switch between different Kotlin versions. As new versions of Kotlin are continuously being developed, the version you see in the drop-down might differ from what you see in your Kotlin Playground. It’s always recommended to use the latest version, which is the default for a new Kotlin Playground session.

Next to it, you’ll find the Kotlin Platform drop-down, which allows you to switch between different target platforms for your Kotlin workspace. You can see that there’re many target platforms to choose from, but in this course, you’ll be using the JVM.

To the right of the Kotlin Platform drop-down, you’ll find the Program Arguments box. This box is editable, and it allows you to pass external data to your Kotlin program. Program arguments are a collection, and using it is optional. Enter 1 and 2 separated by a space. Modify the code to take in these program arguments and print them to the console.

fun main(args: Array<String>) {

Run the code and it prints 1 and 2.

On the same bar, you’ll find the Copy Link button, which creates a short URL for your current session. You can paste this URL into another browser session and continue writing your code from there.

Next to it is the Share Code button, which allows you to share your code, just like you did with the Copy Link button. However, it has some additional features that let you recreate the entire session, including the theme. You can then embed your code in a browser using the generated URL.

At the far end, you’ll find the Run button, which triggers the program execution. The results are printed in the console below.

Lastly, you have the Search button, which is located at the top-right corner. This isn’t part of the Playground per se, but can be useful in your Kotlin learning journey. Clicking this button opens up a search field, enabling you to search through the Kotlin documentation. You can use this feature to go directly to Kotlin’s intuitive documentation instead of using a search engine.

Kotlin Interactive Shell (ki-shell)

Next up is the Kotlin Interactive Shell or the ki-shell. Visit https://github.com/Kotlin/kotlin-interactive-shell#readme to find instructions on how to install it for your operating system. Start a ki-shell session. Create a variable that holds the message “Upcoming super engineer :]” and prints it to the console:

val message = "Upcoming super engineer :]"

Press Enter. The message, “Upcoming super engineer :]” is printed in the Terminal. To exit, type semi-colon and q: :q. As you can see, the ki-shell has only basic features. It’ll be good for quickly trying out Kotlin features where on a computer with low system resources or one that only provices access to a terminal.

Kotlin REPL

Next is the Kotlin REPL. REPL, pronounced (rep - ul), stands for Read, Evaluate, Print, and Loop. A REPL environment lets you quickly write and test code. You can access Kotlin REPL using the Kotlin binaries, IntelliJ IDEA or Android Studio. For the first option, simply run the kotlin or kotlinc binary in your terminal. As you can see, it looks very much like the ki-shell. The procedure for the last two options are the same. In this demo, you’ll see how to access the Kotlin REPL in Android Studio. Open Android Studio and go to File > New > Project in the menu bar, or click New Project in the Welcome to Android Studio screen. In the New Project screen, click Next followed by Finish. Now, go to Tools > Kotlin > Kotlin REPL (Experimental). It’ll open a console at the bottom of the IDE. At the cursor, recreate the program you wrote earlier:

val message = "Upcoming super engineer :]"

Press Ctrl + Enter (Command + Enter on a Mac)to run the code.

It prints “Upcoming super engineer :]” right below the line you just entered.

You can see that the Kotlin REPL supports syntax highlighting and pasting code snippets of more than one line.

Accessing Kotlin Scratch and Worksheet

Access a Kotlin Scratch or Worksheet by opening a project in IntelliJ IDEA or Android Studio. In an existing project, go to File > New > Scratch File. In the New Scratch File popup window, select Kotlin. It creates a new .kts file and opens it in the editing window within the IDE.

Copy and paste the following piece of code:

val message = "Upcoming super engineer :]"

By default, it runs in Interactive mode. This mode executes code as soon as you’re done typing. Uncheck Interactive mode in the panel above the editing area. Click the green arrow button right above the editing area to run the code.

Hover over a piece of code to see its documentation. This alone is a huge feature you don’t get with the other console-based solutions. Choose a module from the drop-down beside the “Use classpath of module” option to access classes from that module. Your session now becomes a Worksheet since it’s able to access classes within your project.

Using Visual Studio Code

Visual Studio Code has a host of plugins and extensions in its ecosystem. Make sure you have JDK 11 or higher installed. Click the extensions button on the left and type ‘Kotlin Language’ in the search window. Install the extension. Search for the Code Runner extension and install that, too. Restart Visual Studio Code.

After it’s installed, create a Kotlin file called demo.kt and save it. This time, you have to execute the code from the main function. Add the following code:

fun main() {
    val message = "Upcoming super engineer :]"

Right-click anywhere in the workspace and select Run Code. The output is displayed in the output window below.

Visual Studio Code has the most features compared to all the tools you’ve seen so far, thanks to its plugins and extensions ecosystem. But it still lags behind a complete IDE like IntelliJ IDEA.

IntelliJ IDEA

Android Studio is built on IntelliJ IDEA, so the features are exactly the same except for some additions to Android Studio. Open InjtelliJ IDEA and click File > New > Project… or New Project from the Welcome to IntelliJ IDEA screen to create a new project. By default the Language should be set to Kotlin otherwise select it. Enter Demo as the Name for the project anc click Create. A new Kotlin project is created with a program in a Main.kt file that prints “Hello World!” when executed.

Click the small green arrow to the left of the function name and select Run ‘MainKt’ to run the code. The output is just as expected; it prints “Hello World!” in the Run window below. This window displays a console for the running application. When the application is running in debug mode, you get debug information in the Debug window instead.

Looking around, there’s a Project window that shows your project in an organized view at the top left corner of the IDE. At the drop-down, you can switch between many different types of project views. At the bottom, there’s the Version Control window. It shows you loads of vital information about the versioned state of your code. There’s even an integrated Terminal, so you don’t need to open a separate application on your computer if you need Terminal access. Since this is an IDE, you get loads of features that’ll make your work a breeze as you build applications.

That’s all for the various ways to write and run Kotlin programs. Continue to the concluding segment of this lesson.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 3 Next: Conclusion