Instruction 3

The Main Function

In Kotlin, the main function is the entry point to every JVM-based program. This means that the Kotlin compiler will look for this function when your program starts to execute. The main function, fun main() has a specific format. It must be named ‘main’ and shouldn’t take any arguments like this:

fun main() {
  println("main function without arguments.")
}

Or, pass in an array of strings as its argument, like this:

fun main(args: Array<String>) {
  println("main function without arguments.")
}

Anything else won’t be recognized, and your program will result in an error if you run it without the main function. Change the name of the function from main to mains and click the Run button to run the program. You’ll see an error in the console output:

Fig. 5.1 - Kotlin program without a main function
Fig. 5.1 - Kotlin program without a main function

You can have other functions in your program, but there should be only one main function. You did notice that, right? Kotlin main and any other functions are fun. Get it? Arguably, many developers do consider Kotlin fun. :]

Main Function Arguments

The argument for the main function, if available, is an array of strings. An array is a simple data structure that comprises data of the same type, in this particular case, strings. A string is computer science speak for a word or even a sentence, so an array of strings is a list of words or sentences. An array of strings means that any characters you pass to the program through the program arguments will be seen as a string. No other function will receive these arguments except the main function.

One other note about the array of strings argument - since every character is converted into a string, you’ll have to convert them into other data types if that’s what you expect to work with. For instance, “5” is still a word but can be converted to the number 5 if needed.

Finally, if you omit the arguments when defining the main function, it simply means you can’t access whatever arguments you pass into the program.

Learning the Difference Between Kotlin Programs and Android Projects

When creating Android applications, you have the option to write the entire program in Kotlin. Writing a program in Kotlin and targeting the Android platform is building a Kotlin Android project. Although other programming languages, such as Java and Dart, can be used to build Android projects, Kotlin is the preferred language for developing Android applications.

However, there’s a key difference between a purely Kotlin program and an Android project. In a purely Kotlin program, the entry point is the main function. But in an Android project, the starting point is determined by the AndroidManifest.xml file. The Android Manifest is a configuration file that describes the Android project, including its name, version, starting point, and allowed permissions.

Therefore, even though an Android application is written in Kotlin, it doesn’t have a main function. Instead, the AndroidManifest.xml file specifies which class or file will become the starting point for the application. Below is an example of how an Android Manifest file looks:

Fig. 5.2 - AndroidManifest.xml file
Fig. 5.2 - AndroidManifest.xml file

In the image above, for instance, the entry point to the Android application is the MainActivity class.

For Android development, the official Integrated Development Environment (IDE) is called Android Studio. Android Studio is based on IntelliJ IDEA, which is software created by JetBrains. It is possible to create Android apps without utilizing Android Studio, though. Android provides a command-line interface (CLI). The CLI can be used by any program to debug Android application development.

Finally, Kotlin programs use the Kotlin Standard Library, stdlib. In contrast, Android projects use Android-specific libraries and resources. The Kotlin stdlib is a collection of classes and functions. These classes and functions help with common programming tasks. It alos includes other tools and data types to help build programs. The stdlib is designed to work with Java libraries and frameworks. You can even use small portions of the stdlib, adding more parts to your project over time.

See forum comments
Download course materials from Github
Previous: Instruction 2 Next: Demo