 | Namespace computer science: Encyclopedia II - Namespace computer science - Use in common languages
Namespace computer science - Use in common languages
In C++, a namespace is defined with a namespace block.
namespace foo {
int bar;
}
Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of namespace foo, bar must be written foo::bar. C++ includes another construct which makes this verbosity unnecessary. By adding the line
using namespace foo;
to a piece of code, the prefix foo:: is no longer needed.
Code that is not explicitly declared within a namespace is considered to be in the default namespace.
Namespaces in C++ are hierarchical. Within the hypothetical namespace food::fruit, the identifier orange refers to food::fruit::orange if it exists, or if not food::orange if it exists. If neither exist, orange refers to an identifier in the default namespace.
Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire standard library is defined within namespace std, and in earlier standards of the language, in the default namespace.
In Java, the idea of a namespace is embodied in packages. All code belongs to a package, although that package need not be explicitly named. Code from other packages is accessed by prefixing the package name before the appropriate identifier, for example class String in package java.lang can be referred to as java.lang.String (this is known as the fully qualified class name). Like C++, Java offers a construct which makes it unnecessary to type the package name (import). However, certain features (such as reflection) require the programmer to use the fully qualified name.
Unlike C++, namespaces in Java are not hierarchical as far as the syntax of the language is concerned. However, packages are named in a hierarchical manner. For example, all packages beginning with java are a part of the Java platform, the package java.lang contains classes core to the language, and java.lang.reflect contains core classes specifically relating to reflection.
In Java (as well as Ada, C#, and others), namespaces/packages express semantic categories of code. For example, in C#, namespace System contains code provided by the system (the .NET framework). How specific these categories are and how deep the hierarchies go differ from language to language.
Although it is not a programming language, XML makes extensive use of namespaces.
Other related archives.NET, Ada, C, C#, C++, DTD, Emulating namespaces, Function, Java, Java platform, JavaScript Namespaces, Libpng, Packages in Java, Python, Scope (programming), URI, W3C, XML, XML Schema, class, computer languages, computer programs, documents, identifiers, modular, name resolution, namespace, object lifetime, packages, programming languages, reflection, scopes, tree, xhtml
 Adapted from the Wikipedia article "Use in common languages", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki |