1. How do I run kotlin
There are about three methods, which are introduced one by one.
The first method is to use IntelliJ IDEA.
This is JetBrains’ flagship IDE development tool and supports Kotlin very well. Create a Kotlin project directly in IntelliJ IDEA to run the Kotlin code independently. The downside of this approach is that you have to download and install an IDE tool, which is a bit of a hassle, so we won’t use this approach here.
The second method is to run the Kotlin code online.
To get a quick experience with Kotlin programming, JetBrains provides a website where you can run Kotlin code online at try.kotlinlang.org.
Just click on the “Run” button in the top right to Run the Kotlin code, very simple. However, one of the major disadvantages of running the Kotlin code online is that it is sometimes very slow to access the site using a domestic network and often does not open, so we are not going to use this method for the sake of learning stability.
The third way is to use Android Studio.
Unfortunately, Android Studio, as a dedicated tool for developing Android applications, can only create Android projects, not Kotlin projects. It doesn’t matter, we can open any Android project, write Kotlin’s main() function inside, and run the Kotlin code independently.
Here we create a new empty project and create a new Kotlin file, as shown in the following 3 images
Next, we write a main() function in the LearnKotlin file and print a line of log, as shown below. (Click the right triangle of 1 or 2 to run it)
As you can see, Hello Kotlin! This means that our code executed successfully.
So you might ask yourself, well, in the training we talked about not using println(), but you should use Log, but why did we use println()?
This is because Log is a logging utility class provided in Android, and we’re running Kotlin code in isolation, independent of Android, so we can’t use Log. This is how you run the Kotlin code independently in Android Studio
We’ll use this method later to run and test what Kotlin learned.