<<

Kotlin main() function

Kotlin main()

The main() function in Kotlin is the entry point to a Kotlin program.

Kotlin main() function can be related to main() function in Java programming or .

Kotlin supports both procedural programming and object oriented programming. If you have worked with some of the procedural languages, you may know that main() is the entry point to a program.

You can write other entities of a program, like a class, or some other functions next to the main function in your Kotlin file.

Kotlin Program with main() Function

Following is a pictorial representation of the Kotlin file with main() function. There is also a data class, and some helper functions, but they are not in focus of this tutorial. We shall discuss about main() function in detail.

If you write a Kotlin file with no main() function, that could be only used as a package or library. But to run a Kotlin File, that file should contain the main() function. Example 1 – Kotlin main()

Following is an example Kotlin program, with main() function.

This Kotlin program has a mina() function, some usual function, and a data class.

Kotlin Program – example.kt

/** * Created by www.tutorialkart.com * main function in kotlin example program */

fun main(args: Array) { val user1 = User(name="Yogi", age=27) printUser(user1) }

fun printUser(user: User){ println(user) }

data class User(val name: String, val age: Int);

When you run the above program, the program control start with the main function. It first loads the arguments passed while running the program to the args Array. And then it executes the program statements of main() function one by one.

When you run the program, you should get the following output in your console.

Output

User(name=Yogi, age=27)

Conclusion

In this Kotlin Tutorial, we have learned about main() function and its importance in a Kotlin application.

Kotlin Java

✦ Kotlin Tutorial

Getting Started ✦ Setup Kotlin(Java) Project

✦ Kotlin Example Program

✦ Convert Java to Kotlin

➩ Kotlin Main Function

✦ Kotlin Loops

✦ Kotlin For Loop

✦ Kotlin While, Do While Loops

✦ Kotlin Repeat

✦ Kotlin Ranges

✦ Kotlin When

Object Oriented Concepts

Classes

✦ Kotlin - Class, Primary and Secondary Constructors

✦ Kotlin Sealed Class

✦ Kotlin Data Class

✦ Kotlin Enum

✦ Kotlin - Extension Functions

Inheritance

✦ Kotlin Inheritance

✦ Kotlin Override Method of Super Class

Abstraction

✦ Kotlin Abstraction

✦ Kotlin Abstract Class

✦ Kotlin - Interfaces

✦ Kotlin Null Safety

Exception Handling

✦ Kotlin Try Catch

✦ Kotlin Throw Exception

✦ Kotlin Custom Exception

Fix Compilation Errors Fix Compilation Errors

✦ Kotlin - Variable must be initialized

✦ Kotlin - Primary Constructor call expected

✦ Kotlin - Null can not be a value of a non-null type String

✦ Kotlin - Cannot create an instance of an abstract class

Kotlin - String Operations

✦ Kotlin - Compare Strings

✦ Kotlin - Replace String

✦ Kotlin - Split String

✦ Kotlin - Split String to Lines

✦ Kotlin - String Capitalize

Kotlin - Functions

✦ Kotlin Function - Default Arguments

✦ Kotlin - Use Function

Kotlin Collections

Kotlin List

✦ Kotlin List

✦ Kotlin List forEach

Kotlin File Operations

✦ Kotlin - Create File

✦ Kotlin - Read File

✦ Kotlin - Read File as List of Lines

✦ Kotlin - Write to File

✦ Kotlin - Append Text to File

✦ Kotlin - Check if File Exists

✦ Kotlin - Copy a File to Other

✦ Kotlin - Iterate through all files in a directory

✦ Kotlin - Delete Recursively

✦ Kotlin - Get File Extension Kotlin Interview Q/A

✦ Kotlin Interview Questions

Kotlin Android

✦ Kotlin Android Tutorial

Useful Resources

✦ How to Learn Programming