Java 包相当于 .Net 程序集吗?
我是一名 .Net 开发人员,正在开始 Android 的 Java 开发,并且想知道像 .Net 程序集这样的 Java 包的想法是否正确。
I am a .Net developer starting Java development for Android and would like to know if it's correct to think of Java packages like .Net assemblies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不。
最好的比较是与 Java ARchive (Jar) 文件。 Java 使用包来控制命名空间,与 C# 的命名空间非常相似。
这是我比较环境的方式
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 包更类似于命名空间
,程序集更像是一个 jar(我对此不太确定,因为我对 Java 比 .Net 更熟悉...纠正我如果我错了)
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)
Java 包就像 .NET 中的命名空间。
jar 文件相当于 Java 中的程序集。
A Java package is like a namespace in .NET.
The equivalent to an assembly in Java is jar file.
Java 中的包通常意味着类和接口的命名空间,实际上意味着特定的目录结构。有时 .jar 文件也称为包。这可能是最接近装配的一次。 .jar 文件还可以包含其他数据,例如图像,或者基本上是一种文件,因为它只是具有某些特定内容结构的 zip 文件。
无论如何:通常当您阅读与“Java”相关的“包”时,意味着某种名称空间(通过文件夹结构,例如:com.mycompany.myproject)。一些构建工具将构建 .jar 文件的过程称为“打包”,这并没有帮助;-)
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" ;-)
请记住,我远非 Java 专家,但无论如何,我对此的看法如下:
正如其他答案所指出的,包并不等同于程序集。但是,我不完全同意 .jar 等同于程序集的想法。
对我来说,Java 类(包含在 .class 文件中)比 .jar 更接近于程序集。我说这个是因为虽然CLR的加载单元是Assembly(这意味着该Assembly中包含的所有类都会被加载),但JVM的加载单元是一个类(当JVM需要一个类时,ClassLoader不会加载容器 jar 中的所有类,它只加载特定需要的类)。
您可以在此处阅读有关 java 类加载的更多信息:
JVM 何时加载类?
以及此处:
http://www.lansa.com/support/notes/p0294.htm
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