Kotlin GettingStartKotlin
Summarize about Kotlin.
1.1 Hello World
All Kotlin programs start at the main function.
When targeting the JVM, the function will be compiled as a static method in a class with a name derived from the
filename. In the above example, the main class to run would be my.program.MainKt.
1 | package my.program |
1.2. With companion object
Similar to using an Object Declaration, can define at Class lke this.
1 | class Hello { |
The advantage to this method is more self-evident and any other functions you add are scope into the class App.
1.3. With Object Declaration
Can alternatively use an Object Declaration what contains the main function.
1 | object App { |
The advantage to this method is super self-evident and also have singleton instance of App.
1.4. Compile and Run Kotlin Code
As java provide two different commands to compile and run Java code. Same as Kotlin also provide you different
commands.
javac to compile java files. java to run java files.
Same as kotlinc to compile kotlin files kotlin to run kotlin file.