views:

134

answers:

4

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. Thanks!

+3  A: 

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)

froadie
Joshua
+1 Your answer reflects the Wiki http://en.wikipedia.org/wiki/Java_package
John K
@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 French
@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
@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
@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
+7  A: 

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
Kelly French
+1  A: 

A Java package is like a namespace in .NET.

The equivalent to an assembly in Java is jar file.

Nuno Ramiro
+1  A: 

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" ;-)

Horst Gutmann