Package Structure

Until now, we have written all our code in a single file. However, real programs consist of dozens or hundreds of files. In Kotlin, these files are organized into Packages.

Kotlin’s package system is very similar to Java’s, but much more flexible.


What is a Package?

A package is a way to group related code files into folders.

At the very top of a Kotlin file, you declare which package the file belongs to:

package com.myapp

fun main() {
    println("Hello!")
}

package com.myapp tells Kotlin, “This file is located in the com/myapp folder.”


Using Code from Other Packages

To use functions or classes from a different package, you use import:

package com.myapp

import com.myapp.utils.formatName

fun main() {
    println(formatName("kotlin"))
}

import means “I will bring in and use this code.”

In IntelliJ, it automatically adds imports when you use code from another package, so you don’t have to worry!


Creating Your Own Package

my_project/
├── src/
│   └── main/
│       └── kotlin/
│           └── com/
│               └── myapp/
│                   ├── Main.kt           ← Entry point
│                   ├── calculator/
│                   │   └── Calculator.kt ← calculator package
│                   └── greeting/
│                       └── Hello.kt      ← greeting package

calculator/Calculator.kt:

package com.myapp.calculator

fun add(a: Int, b: Int): Int {
    return a + b
}

fun subtract(a: Int, b: Int): Int {
    return a - b
}

Using it in Main.kt:

package com.myapp

import com.myapp.calculator.add

fun main() {
    val result = add(3, 5)
    println(result) // 8
}

Kotlin’s Flexibility

Unlike Java, Kotlin allows you to freely put multiple functions and classes in a single file.

// Utils.kt

package com.myapp.utils

fun formatName(name: String): String {
    return name.uppercase()
}

fun isValidEmail(email: String): Boolean {
    return email.contains("@")
}

data class User(val name: String, val email: String)

In Java, the file name must match the class name, but Kotlin has no such restriction.


Kotlin’s Standard Packages

Kotlin provides frequently used features by default:

PackageDescription
kotlinThe most basic tools (Int, String, println, etc.) - No import needed
kotlin.collectionsData structures like Lists and Maps - No import needed
kotlin.ioInput/output related features
kotlin.mathMath-related features

The reason you could use println() without an import is that it’s part of the kotlin package, which is automatically included!


What a Real Project Looks Like

my_app/
├── build.gradle.kts              ← Build configuration file
└── src/
    └── main/
        └── kotlin/
            └── com/
                └── myapp/
                    ├── Main.kt                ← Program entry point
                    ├── model/                 ← Data structures
                    │   ├── User.kt
                    │   └── Product.kt
                    ├── service/               ← Business logic
                    │   ├── UserService.kt
                    │   └── ProductService.kt
                    └── util/                  ← Toolset
                        └── StringHelper.kt

By dividing packages by role, you can quickly find what you need: “User-related logic is in the service package, and data structures are in the model package!”


Using External Libraries

You can also use libraries created by other developers by adding them to the build.gradle.kts file:

dependencies {
    implementation("com.google.code.gson:gson:2.10.1")
}

Once added, you can import and use them immediately:

import com.google.gson.Gson

fun main() {
    val gson = Gson()
    // Process JSON data...
}

To summarize:

  • Package = A folder to organize related Kotlin files
  • package declaration = Telling Kotlin where this file belongs
  • import = Bringing in code from other packages
  • Kotlin Standard Packages = Features available without import (like println)

By understanding Kotlin’s package system, you can create organized and clean projects!