官术网_书友最值得收藏!

Writing scripts with Kotlin

As previously stated, Kotlin can be used to write scripts. Scripts are programs that are written for specific runtime environments for the common purpose of automating the execution of tasks. In Kotlin, scripts have the .kts file extension appended to the file name.

Writing a Kotlin script is similar to writing a Kotlin program. In fact, a script written in Kotlin is exactly like a regular Kotlin program! The only significant difference between a Kotlin script and regular Kotlin program is the absence of a main function.

Create a file in a directory of your choosing and name it NumberSum.kts. Open the file and input the following program:

val x: Int = 1
val y: Int = 2
val z: Int = x + y
println(z)

As you've most likely guessed, the preceding script will print the sum of 1 and 2 to the standard system output. Save the changes to the file and run the script:

kotlinc -script NumberSum.kts
A significant thing to take note of is that a Kotlin script does not need to be compiled.
主站蜘蛛池模板: 仙居县| 瑞金市| 南召县| 通渭县| 黎城县| 望都县| 乌兰察布市| 石渠县| 奉化市| 准格尔旗| 昌吉市| 牡丹江市| 昌黎县| 长寿区| 盘锦市| 文山县| 奉化市| 萨嘎县| 青阳县| 乳源| 夹江县| 丰宁| 湟中县| 上饶市| 边坝县| 双鸭山市| 读书| 海门市| 固始县| 玛纳斯县| 庆云县| 利津县| 永城市| 周宁县| 涪陵区| 永州市| 贵州省| 富平县| 阳原县| 灵寿县| 凯里市|