The Kotlin system should print out a greeting message and the prompt “>>> ”
$ kotlinc
Welcome to Kotlin version 1.3.50 (JRE 1.8.0_152-b16)
Type :help for help, :quit for quit
>>>
If you type in a statement, the system will try to execute it.
If the statement is valid, the system executes the statement
and possibly prints something out as a side-effect.
If the statement is invalid, the system prints out an error message.
It then prints out the prompt.
print()
or println()
function.run the script by typing
$ kotlinc -script file.kts
Compile the source file by typing
$ kotlinc file.kt
Run the class file by typing
$ kotlin FileKt
For deployment, you can compile the source to a “.jar” file by typing
$ kotlinc -include-runtime -d FileKt.jar file.kt
This will produce a jar file named “FileKt.jar” which can be executed by the command
$ java -jar FileKt.jar
Here is the standard first Kotlin program.
/* Prints "Hello, World" to standard output. */
fun main() {
// This is line comment
println("Hello, World")
}
A multi-line comment can span more than one line. It starts with /* and ends with */ and can nest.
This program shows how to process command line arguments.
/*
* Prints "Hi, Bob. How are you?" where "Bob" is replaced by the
* command-line argument.
*/
fun main(args: Array<String>) {
print("Hi, ")
print(args[0])
println(". How are you?")
}
main
function.The parameter args
is an array of strings corresponding to the command line arguments typed in by the user when they invoke the program.
writing
"Ho" * 3
print()
, or println()
function.