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.
5 Answers
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

- 12,198
- 10
- 63
- 93
-
1I'll add that an Assembly in .Net has more subtle access modifiers than a Jar in Java (at least, that's how I understand it). This answer explains one such detail: http://stackoverflow.com/a/9808237/1168342 – Fuhrmanator Mar 12 '13 at 03:13
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)

- 79,995
- 75
- 166
- 235
-
2There is zilch-all preventing me from adding classes to the java.lang namespace by creating a new package. One of the ways for breaking checked exceptions called for doing exactly that (overriding Exception & RuntimeException at compile time). – Joshua Sep 02 '10 at 16:54
-
@Joshua, adding classes to the java.lang package is possible only if you put the class in its own jar. It would also only work for the added class. If you were trying to replace the Exception class, I'm not sure its possible because the JRE loads that itself even if there is no CLASSPATH defined. – Kelly S. French Sep 02 '10 at 16:58
-
3@Joshua: I'm not sure what you mean, or why your comment is being upvoted. Adding classes to the java.lang "namespace" (not sure what your def of namespace is here) would not require "creating a new package." In fact "creating a new package" doesn't even make sense from a Java standpoint; a package isn't an entity, it's a naming structure. – Mark Peters Sep 02 '10 at 17:03
-
1@Joshua: But in terms of malicious/fun/voodoo things you can do with that, yes I think it's possible but there isn't "zilch-all" preventing you from overriding Exception and RuntimeException...you would have to override the bootstrap classloader for starters. – Mark Peters Sep 02 '10 at 17:04
-
@Mark: that's true at runtime but the compiler doesn't know that. And the particular procedure I was following only required overriding at compile time, not runtime. – Joshua Sep 02 '10 at 19:51
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?

- 1
- 1

- 895
- 1
- 9
- 8
A Java package is like a namespace in .NET.
The equivalent to an assembly in Java is jar file.

- 1,756
- 1
- 11
- 4
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" ;-)

- 10,910
- 2
- 28
- 31