Maths encyclopedia and lessons  
Search

Mathematics Encyclopedia and Lessons

 
     
 

Lessons

Popular
Subjects

algebra
arithmetic
calculus
equations
geometry
differential equations
trigonometry
number theory
probability theory
more
 

References

applied mathematics
mathematical games
mathematicians
more
 
 

Namespace (programming)

In many programming languages, a namespace is a context for identifiers. Some modern programming languages such as C++ or Java can handle multiple namespaces within the language.

History

In early programming languages, such as C, namespaces were defined implicitly by the semantics of a program. For example, an identifier defined within a function only has meaning within the context of that function. These implicitly defined namespaces were inextricably linked with the ideas of visibility, accessibility, and lifetime (see scope).

As programming progressed and software projects became more complex, a need was seen for a language construct which separated visibility from context. Before namespaces, naming collisions were avoided by prefixing characters to an identifier in various nonstandard ways. This practice encumbered readability. Therefore, namespaces have been added to most modern languages.

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.

See also

01-04-2007 01:18:14
The contents of this article are licensed from Wikipedia.org
under the GNU Free Documentation License. How to see transparent copy