What's the difference between “package” and “module”?

前端 未结 5 829
你的背包
你的背包 2020-12-04 23:42

I use Java where we only have packages. I know there are other programming languages that also include modules.

What\'s the difference?

相关标签:
5条回答
  • 2020-12-05 00:16

    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 packages, which are basically just a mechanism for organizing Java classes, interfaces etc into namespaces. They require hierarchical structure. packages don't have first class status. It might also be worth noting that superpackages proposed for Java 7 are also sometimes referred to as modules.

    • Modula: modules, same in concept to Java's packages. Don't require hierarchical structure.

    • C#: namespaces, same in concept to Java's packages. Don't require hierarchical structure.

    • C++: namespaces, As the name indicates, these are just namespaces. Don't require hierarchical structure.

    • Haskell: modules, same in concept to Java's packages.

    • Scala: packages in Scala are same as packages in Java, except that they don't require hierarchical structures. A few more restrictions like one-public-class-per-file have also been relaxed. objects in Scala are also referred to as modules, and they also enjoy the first-class status.

    • F#: namespaces in F# are same as C# namespaces. Besides namespaces, F# also has modules which are implemented at CLR level as .NET classes with static methods. They aren't first-class entities.

    0 讨论(0)
  • 2020-12-05 00:19

    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.

    0 讨论(0)
  • 2020-12-05 00:23

    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.

    0 讨论(0)
  • 2020-12-05 00:36

    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.

    0 讨论(0)
  • 2020-12-05 00:40

    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.

    0 讨论(0)
提交回复
热议问题