I use Java where we only have packages. I know there are other programming languages that also include modules.
What\'s the difference?
instanceofTom's comment nailed it - Different languages have different definitions of package and module. Therefore there's no language agnostic answer to this question.
I'll try to answer it from the perspective of some of the languages I know:
Java: It has a concept of package
s, which are basically just a mechanism for organizing Java classes, interfaces etc into namespaces. They require hierarchical structure. package
s don't have first class status. It might also be worth noting that superpackage
s proposed for Java 7 are also sometimes referred to as module
s.
Modula: module
s, same in concept to Java's package
s. Don't require hierarchical structure.
C#: namespaces
, same in concept to Java's package
s. Don't require hierarchical structure.
C++: namespaces
, As the name indicates, these are just namespaces. Don't require hierarchical structure.
Haskell: module
s, same in concept to Java's package
s.
Scala: package
s in Scala are same as package
s in Java, except that they don't require hierarchical structures. A few more restrictions like one-public-class-per-file have also been relaxed. object
s in Scala are also referred to as modules, and they also enjoy the first-class status.
F#: namespaces
in F# are same as C# namespace
s. Besides namespace
s, F# also has modules
which are implemented at CLR level as .NET classes with static methods. They aren't first-class entities.
It seems that, as other answers state, the notion of module varies from one language to another, some even not having it (but a corresponding counterpart).
However, it seems that nowadays modules is more a concept and refers to a set of source code files that can be shared independently. Following this definition, Java jar files are not modules as they contain bytecode compiled files (.class files). A module system complying with this definition is to be introduced in Java 9 (see this article) and is present in other languages that have module or package managers, often directly naming modules packages (Go lang/go tool, Python/pip, PHP/Composer...) where package generally are packed modules.
It's hard to compare semantics in the void. (What other languages do you mean?) A "module" might be analogous to a Java class, or a Java package, or something else entirely, depending on that other language. Typically since "modules" tend to be from procedural languages, I'd lean toward saying Java class, but I think the line is very fuzzy at that point and you could argue package quite convincingly.
A package is more akin to a C++ namespace than a module. A module is more akin to an enclosing class than to a package.
Java Platform Module System or JPMS was introduced when java-9 was released. Starting from that time Java has both packages
and modules
. So what is the difference between them?
What is a Package?
A package is a namespace that organizes a set of related classes and interfaces.
What is a Module?
A module is a collection of related Java packages and associated resources with a descriptor file, which contains information about which packages/resources are exposed by this module, which packages are used by current module and some other information.
We can thing of a Java Module as a higher level of aggregation above packages. A Module allows you to organize a few packages into one single logical unit and to distribute them as one whole system. As well JPMS provides a way to control which packages are visible to users.