Is a Java package the equivalent of a .Net assembly?

試著忘記壹切 提交于 2019-11-27 19:17:37

No.

The best comparison would be with a Java ARchive (Jar) file. Java uses packages to control the namespace, and is very similar to C#'s Namespaces.

Here is how I'd compare the environments

Java                    .Net
====                    ====
Class                   Class
Package                 Namespace
Jar                     Assembly

No, I think a Java package would be more similar to a namespace

And an assembly would be more like a jar (I'm not so sure about this, as I'm much more familiar with Java than .Net... correct me if I'm wrong)

A Java package is like a namespace in .NET.

The equivalent to an assembly in Java is jar file.

A package in Java usually means just a namespaces for classes and interfaces, which in reality means a specific directory structure. Sometimes a .jar file is also referred to as a package. This is probably the closest you get to an assembly. A .jar file can also contain other data like images, or basically an kind of file since it is just a zip again with some specific content structure.

In any case: Usually when you read "package" in relation to "Java", some kind of namespaces (via folder structure, e.g.: com.mycompany.myproject) is meant. It doesn't help that some build tools refer to the process of building .jar file as "packaging" ;-)

Xose Lluis

Bear in mind that I'm far from a Java expert, but anyway, there it goes my take on this:

As pointed out by other answers, a package is not the equivalent to an Assembly. However, I don't fully agree with the idea of a .jar being the equivalent to an Assembly. To me, a Java class (contained in a .class file) is closer to an Assembly than a .jar is. I'm saying this cause while the load unit for the CLR is the Assembly (which means all classes contained in that Assembly get loaded), the load unit for the JVM is a class (when the JVM needs a class, the ClassLoader does not load all the classes in the container jar, it just loads that specific needed class).

You can read more about java class loading here: When does the JVM load classes?

and here: http://www.lansa.com/support/notes/p0294.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!