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.
import package-name
Let’s see how we import a package in Kotlin, in the following code snippet:
// import packageimport kotlin.math.*fun main() {// access method defined in packagevar sqrtOf4 = sqrt(4.0)println(sqrtOf4)}
math
package of Kotlin.main
function.sqrt()
method present in the math
package to calculate the square root of 4.0
.We can see the square of 4.0
, that is, 2.0
on the console.