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

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.
主站蜘蛛池模板: 丹阳市| 大港区| 辽宁省| 泾川县| 贵州省| 福建省| 卢龙县| 德令哈市| 甘谷县| 长沙县| 泰宁县| 惠水县| 安吉县| 嘉兴市| 盐亭县| 灵璧县| 贵南县| 那曲县| 康保县| 永靖县| 扬中市| 淄博市| 彭水| 盐边县| 大港区| 赤壁市| 义乌市| 逊克县| 阳江市| 安塞县| 印江| 塔城市| 无极县| 鄂托克前旗| 南雄市| 西乌珠穆沁旗| 青神县| 灌南县| 泰宁县| 太康县| 确山县|