Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "package" and "module" in Kotlin?

In terms of Kotlin, what is the difference between a package and a module?

When it comes to visibility modifiers for top-level code, the internal modifier only allows accessibility for code inside the same module. This makes it seem like package and module are the same thing but I'm not certain.

The question here does not answer my question as it does not bring modules and packages into the same context.

like image 995
Jisip Avatar asked Aug 16 '19 22:08

Jisip


People also ask

What is the difference between package and module?

A Python package defines the code as a separate unit for each function when using a library. While the modules themselves are a distinct library with built-in functionality, the advantage of packages over modules is their reusability. So this is the difference between a module and a package in Python.

What is module in Kotlin?

A Kotlin module is a set of Kotlin files which are considered to be interdependent and must be handled together during compilation. In a simple case, a module is a set of files compiled at the same time in a given project. A set of files being compiled with a single Kotlin compiler invocation.

What are modules and packages?

A module is a file that contains a Python script in runtime for the code specified to the users. A package also modifies the user interpreted code in such a manner that it gets easily operated in the runtime.

What is the difference between package and module in Java?

A package contains many classes (. java files) and is basically a folder in your Java projects. A module contains many packages (folders with . java files).


1 Answers

Short answer: packages collect related classes, and correspond roughly to directories; while modules are a much higher level and correspond to projects and/or compiler runs.

Longer answer:

In Java and Kotlin, classes are arranged into packages.  They're set using the package directive at the top of each file.  (In Java, this corresponds exactly to the directory structure in which they're stored; that's common in Kotlin too, though not strictly required.)

They provide a way of grouping related classes: you can refer to classes (and top-level functions and fields) in the same package directly, while all other classes need to be imported, or their fully-qualified names (package.packageclass) used.  And in the most recent versions of Java, you can ‘seal’ a package, which means no-one else can add classes to it later on.

Modules, on the other hand, are new to Kotlin.  They are a much higher-level concept, collecting together all the classes in a program or library.  (Some IDEs and tools call this a ‘project’ or ‘source set’.)  All the files in a module must be compiled together, and the results are usually collected into a .jar (or .war) file.

A big system might consist of a handful of modules, but each of those could contain many tens of packages.

like image 177
gidds Avatar answered Oct 18 '22 14:10

gidds