← Kotlin EnglishChapter 01 of 13

Introduction to Kotlin

## Learning Objectives - Understand Kotlin history and design goals - Set up Kotlin development environment - Write and run your first Kotlin program - Understand Kotlin syntax basics ## What is Kotlin? Kotlin is a modern, statically-typed programming language developed by JetBrains. It runs on the JVM and can also be compiled to JavaScript or native code. ```kotlin fun main() { println("Hello, World!") } ``` ## Why Kotlin? ### Key Features - **Concise** - Reduces boilerplate code significantly - **Safe** - Built-in null safety to prevent NullPointerException - **Interoperable** - Works seamlessly with existing Java code - **Tool-friendly** - Excellent IDE support in IntelliJ IDEA ### Kotlin vs Java | Feature | Java | Kotlin | |---------|------|--------| | Null safety | Optional @Nullable | Built-in | | Data classes | Verbose | Simple `data class` | | Extension functions | Not available | Yes | | Coroutines | External libraries | Built-in | | Default arguments | Overload methods | Yes | ## Installing Kotlin ### Option 1: IntelliJ IDEA (Recommended) Download from ### Option 2: Command Line ```bash # macOS with Homebrew brew install kotlin # Linux with SDKMAN sdk install kotlin # Verify installation kotlin -version ``` ### Option 3: Online Playground Try Kotlin at ## Your First Program ### File: HelloWorld.kt ```kotlin fun main() { println("Hello, World!") } ``` ### Compile and Run ```bash kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar java -jar HelloWorld.jar ``` Or run directly: ```bash kotlin HelloWorld.kt ``` ## Kotlin Program Structure ```kotlin fun main() { // Entry point function val message = "Hello" // Immutable variable println(message) // Print to console } ``` ### Components 1. **Function** - `fun` keyword declares functions 2. **Variables** - `val` for immutable, `var` for mutable 3. **Statements** - End with semicolon (optional) ## Comments ```kotlin // Single-line comment /* * Multi-line comment */ /** * Documentation comment (KDoc) */ ``` ## The main Function ```kotlin fun main() { println("Hello!") } ``` With command-line arguments: ```kotlin fun main(args: Array) { for (arg in args) { println(arg) } } ``` ## System Output ```kotlin // Print with newline println("Hello") // Print without newline print("Hello ") print("World") // String interpolation val name = "Alice" println("Hello, $name!") // Expression in string println("Sum: ${1 + 2}") ``` ## Reading Input ```kotlin fun main() { print("Enter your name: ") val name = readLine() println("Hello, $name!") } ``` ## IDEs for Kotlin ### Popular IDEs - IntelliJ IDEA (recommended, comes with Kotlin plugin) - Android Studio (for Android development) - Eclipse with Kotlin plugin - VS Code with Kotlin extension ## Kotlin Versions ```bash kotlin -version # Check installed version ``` | Version | Year | Feature | |---------|------|---------| | Kotlin 1.0 | 2016 | First stable release | | Kotlin 1.1 | 2017 | Coroutines | | Kotlin 1.3 | 2018 | Contracts, stdlib rewrite | | Kotlin 1.5 | 2021 | Stable inline classes | | Kotlin 1.9 | 2023 | K2 compiler beta | | Kotlin 2.0 | 2024 | K2 compiler stable | ## Summary - Kotlin is a modern JVM language developed by JetBrains - It is concise, safe, and fully interoperable with Java - `fun` keyword declares functions - `println()` prints to console with newline - String interpolation uses `$variable` or `${expression}` - Use IntelliJ IDEA or kotlinc for development

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →