Instruction 2

Learning About Kotlin Playground

As you progress through this module, you’ll be using the Kotlin Playground to master the basics of Kotlin. Among the numerous options displayed earlier, Kotlin Playground is the simplest and swiftest way to get started with Kotlin.

To launch a new playground session, visit https://play.kotlinlang.org. This interface is pretty straightforward and has fewer features compared to a comprehensive IDE such as Android Studio.

Fig. 4.1 - Kotlin Playground
Fig. 4.1 - Kotlin Playground

Here’s an overview of its main features and how they operate:

Kotlin Version

This specifies the version of Kotlin for your session. The Kotlin library is constantly updated, meaning the version you’re currently using may differ from the latest one in the course. It’s always best to work with the most recent version. However, there may be instances where you need to use an older version. You can click the drop-down button and select your preferred version in such cases.

Fig. 4.2 - Kotlin Playground: Kotlin version dropdown
Fig. 4.2 - Kotlin Playground: Kotlin version dropdown

Platform Version

Kotlin is a versatile and powerful programming language that compiles on various platforms, including the JVM platform, Javascript environment, Web Assembly, and many others. This means that Kotlin programs can operate on almost any platform. Kotlin has a multiplatform toolset that facilitates this. However, for this course, you’ll write your code targeting the JVM platform. The JVM is available on a wide range of devices, including phones, television sets, smartwatches, and many more.

Fig. 4.3 - Kotlin Playground: Kotlin platform dropdown
Fig. 4.3 - Kotlin Playground: Kotlin platform dropdown

Program Arguments

Program arguments are data that a program accepts as part of its parameters or conditions prior to execution. They’re usually used to customize the program’s behavior. You can input arguments in the Program arguments box, separated by a space.

Fig. 4.4 - Kotlin Playground: Program arguments box
Fig. 4.4 - Kotlin Playground: Program arguments box

To modify the main function and display your program arguments, enter “John” followed by a space and “5” in the Program Arguments box.

Fig. 4.5 - Kotlin Playground: Arguments entry
Fig. 4.5 - Kotlin Playground: Arguments entry

Now, update the main function with the following code:

fun main(args: Array<String>) {

}

Modify the code in the body of the function to display your name and age instead of “Hello, world!!!”. Update your code as follows:

fun main(args: Array<String>) {
  println("My name is ${args[0]} and I'm ${args[1]} years old.")
}

Once you run the program, you’ll notice that the values you entered in the Program arguments box have been transferred to the program. This is one way of making your program dynamic. How cool is that? :]

My name is John and I'm 5 years old.

Now, use the println method, but split the text into two different calls:

fun main(args: Array<String>) {
  println("My name is ${args[0]} ")
  println("and I'm ${args[1]} years old.")
}

Run the program. You’ll notice that the println methods adds a newline after it prints:

My name is John
and I'm 5 years old.

Modify your code to use the print method on the split text:

fun main(args: Array<String>) {
  print("My name is ${args[0]} ")
  print("and I'm ${args[1]} years old.")
}

Run the program. You’ll notice that the print method does not add a newline. The output looks just like the original println code:

My name is John and I'm 5 years old.

You can also display the name and age this way:

fun main(args: Array<String>) {
  val message = "My name is ${args[0]} and I'm ${args[1]} years old."
  println(message)
}

Run the code again. You will see the same output as the original code:

My name is John and I'm 5 years old.

Using the Search Button

The search button is in the top-right corner. It searches through the documentation, making it a valuable tool as you learn Kotlin. You don’t have to open a new tab or utilize any search engine. Kotlin’s documentation is user-friendly, with embedded examples that you can run to test out whatever concepts you’re attempting to grasp.

Fig. 4.7 - Kotlin Playground: Search button
Fig. 4.7 - Kotlin Playground: Search button

Using the Copy Link

This button generates a shortened URL that you can share with others or open on another tab or device to continue your session.

Fig. 4.8 - Kotlin Playground: Copy link button
Fig. 4.8 - Kotlin Playground: Copy link button

Sharing Code

This button is similar to the Copy Link button, with some additional customizations that enable you to embed the link on a webpage, specify certain lines of code to include, change the theme, and other convenient features.

Fig. 4.9 - Kotlin Playground: Share code button
Fig. 4.9 - Kotlin Playground: Share code button

You can select the Kotlin version to create your program, share your code with others, specify additional data for the program to begin with (Program arguments), and, most importantly, run your Kotlin programs.

Running

The Run button is what you use to execute code in the Kotlin Playground. For the most part, throughout this module, you’ll be writing code in the editor, executing it with the Run button, and monitoring the output in the console beneath it.

Fig. 4.10 - Kotlin Playground: Run button
Fig. 4.10 - Kotlin Playground: Run button

Kotlin Playground is designed exclusively for the Kotlin language and doesn’t provide functionalities for creating complete software applications. This is where Android Studio comes in.

Android Studio

Android Studio has all of Kotlin Playground’s features and more. It provides a comprehensive programming environment ideal for developing simple to complex Android applications. With Android Studio, you can write code, organize it into modules, test your code, create artifacts, manage version control, and much more.

Fig. 4.11 - Kotlin Playground: Android Studio scratch workspace
Fig. 4.11 - Kotlin Playground: Android Studio scratch workspace

Now that you’re familiar with the various Kotlin development environments let’s focus on the Kotlin language itself. If you look at the code you’ve used in all the platforms you saw earlier, you’ll notice two primary differences: either you write your code directly or within the main function. So, what’s the main function? Keep reading to learn more about this mysterious function.

See forum comments
Download course materials from Github
Previous: Instruction 1 Next: Instruction 3