What's the difference between "package" and "module"?

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.


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.


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.