0
0
Kotlinprogramming~5 mins

Kotlin REPL and script mode

Choose your learning style9 modes available
Introduction

Kotlin REPL and script mode let you quickly try out Kotlin code without making a full project. It helps you learn and test ideas fast.

You want to test a small piece of Kotlin code quickly.
You are learning Kotlin and want to experiment with commands one by one.
You want to write a simple Kotlin script to automate a task.
You want to check how Kotlin functions or expressions behave without creating a full app.
Syntax
Kotlin
REPL: Run `kotlinc-jvm` in terminal and type Kotlin code line by line.

Script mode: Save code in a file with `.kts` extension and run with `kotlinc-jvm -script filename.kts`.

REPL stands for Read-Eval-Print Loop. It reads your code, runs it, shows the result, and waits for more.

Script mode runs Kotlin code from a file without needing a full project setup.

Examples
Start REPL by typing kotlinc-jvm in your terminal, then type Kotlin commands like println.
Kotlin
kotlinc-jvm
println("Hello from REPL!")
Save this code in a file named hello.kts and run it with kotlinc-jvm -script hello.kts.
Kotlin
// hello.kts
println("Hello from script!")
Sample Program

This script sets a variable and prints a greeting. Run it with kotlinc-jvm -script greet.kts.

Kotlin
// Save as greet.kts
val name = "Friend"
println("Hello, $name!")
OutputSuccess
Important Notes

In REPL, you can define variables and functions and use them in following lines.

Script files use the .kts extension to tell Kotlin to run them as scripts.

REPL is great for quick tests, scripts are better for reusable small programs.

Summary

Kotlin REPL lets you run code interactively line by line.

Script mode runs Kotlin code saved in .kts files.

Both help you try Kotlin quickly without full project setup.