There's no such term as "namespace" in Java - a package acts as a namespace in Java though, in terms of providing a scope for names. It's also part of the accessibility model.
Programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A top level type is accessible (§6.6) outside the package that declares it only if the type is declared public.
EDIT: Okay, after the clarification: a Java package is similar to a C# namespace - except that it has an impact on accessibility, whereas in C# namespaces and accessibility are entirely orthogonal.
In java you can apply various access specifiers to classes which will have impact on your packages.
protected : accessible to same package and to its subclasses in another package,
default : accessible to same package,
public : universally accessible,
private : not even accessible by the same package.
These type of access specifiers are not applicable to namespace in c sharp
A namespace is just like a new folder, all subfolders are sub-namespaces. If we consider a namespace as a function like we have a namespace advertising under marketing namespace then we use marketing.advertising.adsclass.adsmethod. Very easy to solve a problem. Java has same method via package but complex for new comers.
In C#
namespace marketing{
class admissions{
int admissions_method(){
}
}
namespace advertising{
class advertisement{
void ad_in_newspaper( int no_of_lines){
}
void ad_on_tv(int seconds){
}
}
}
To use in client class
using marketing;
using marketing.advertising;
In java you use same method. You pack multiple classes in one package and use it many times. It increases uniqueness. You write once and use many times. Related classes in one package. No need to code many times.
While they are fundamentally different, they can be used in the same way and are intended for the same use. Both a C# namespace and a Java package are meant to organize classes.
They do have some clear differences though.
[1] Virtual?
In Java, the package name must be identical to the folder path relative to the source folder. For example; Imagine we have a class called Hello;
public class Hello { }
We want Hello to be in the com.example.hello package. To accomplish this, we have to first move the Java file for public class Hello (In our case Hello.java) to the folder <source folder>/com/example/hello. (Replace <source folder> with the source folder, for example; src for normal Java, src/main/java for Maven and Gradle, etc.). The second thing we have to do is add package com.example.hello to the beginning of our Hello.java file.
The reason that Java requires you do this is because the Java Class Loader pulls the classes from the jar based on the <package(with /)>.<class>.class path format.
In C# this is not required. Namespaces are completely virtual there. So lets define Hello again, but this time we want it in the Example.Test namespace.
class Hello { }
Here all we have to do is nest our class declaration in two sets of namespace scopes.
Like this;
namespace Example {
namespace Test {
class Hello { }
}
}
[2] Naming conventions
This is not a major difference in functionality, but it is definitely a notable one.
In Java you often name the packages like this; me.name.app.category Sometimes people also use something like me.name.App.category or other variants, but you rarely see people use Me.Name.App.Category in Java, while that is exactly how most people name their namespaces in C#.
[3] Modules
Another major difference is that Java actually uses packages for modules as well, not just for named categories. I don't know a lot about this topic so I won't explain it as I may give false information.
[4] Access
In Java, packages can also act as a way to limit access to a certain symbol.
For example, lets say that we have a class structure like this;
com
↳ example
↳ hello
↳ Hello (class)
↳ bye
↳ interfaces
↳ IBye (class)
↳ Bye (class)
And the Bye class is declared like this;
package com.example.bye;
public class Bye {
public static void say() {
System.out.println("Bye!");
}
}
And the main method is contained inside of the Hello class.
Okay so right now, we can access Bye.say() from anywhere in the application, because it is marked as public. So in this case we can safely call it from both Hello and IBye.
But what if we didn't want that, what if we only wanted it to be accessible from the package it is in. Then we just need to replace the public in the say() method declaration with nothing. This will prevent any class that is not in that package from accessing the method, field, class or constructor.
There is something that you'll have to note though. Remember the IBye class? Well, technically it is in the same package as Bye, but just deeper. Sadly Java does not account for this so we are not able to access it from sub-packages.
Extra Information
Now there are more access modifiers in Java, but I won't explain them all here.
But there are of course some really useful sites on this topic, so here they are;
In a nutshell
They may look similar and are generally intended for the same purpose, but Java packages are not virtual, have a different naming convention and manage modules and access modifiers.
C#-like emulation
In Java, you can try to replicate the C# namespace system using Nested classes.
Because;
1. Java classes have the same naming convention as C# namespaces.
2. Nested classes are completely virtual and can be moved by just moving the declaration to a different class scope.
So imagine that we wanted to put our Hello class in 'namespace' Example.Test, what we could do is this;
public class Example {
public static class Test {
public static class Hello { }
}
}