In Java the java.lang
package is imported by default.
In kotlin a number of functions and classes are available without being imported, like println
and kotlins Array
, Int
, etc types.
What else is imported by default and where is it defined?
lang package is imported by default. In kotlin a number of functions and classes are available without being imported, like println and kotlins Array , Int , etc types.
Imports. In Kotlin, we use the import declaration to enable the compiler to locate the classes, functions, interfaces or objects to be imported. In Java, on the other hand, we can't directly import functions or methods—only classes or interfaces.
Package-level functions are also known as top-level functions. They are declared directly inside a file without creating any class for them. They are often utility functions independent of any class: UserUtils.kt package com.app.user fun getAllUsers() { } fun getProfileFor(userId: String) { }
Overview. A package is a group of related classes, enums, functions, sub-packages, and so on. To import a package in Kotlin, we use the import keyword, followed by the name of the package that needs to be imported.
Kotlin stdlib has kotlin
root package and its subpackages (see the full list with the content).
It seems not to be documented anywhere which of them are imported by default, but a peek into Kotlin Github sources suggests that these packages are imported for JVM target platform:
java.lang.*
kotlin.*
kotlin.annotation.*
kotlin.jvm.*
kotlin.collections.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
kotlin.io.*
kotlin.coroutines.*
(to be added in Kotlin 1.1, not present in 1.0.4)
I've manually tested them, and the list above is true for Kotlin 1.0.4. And these stdlib packages are not imported by default:
kotlin.comparisons.*
kotlin.concurrent.*
kotlin.properties.*
kotlin.reflect.*
kotlin.reflect.jvm.*
kotlin.system.*
As @Magnus noted, the default imports for JS platform are different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With