FACTOID # 152: Of the eight countries which include the word "democratic" in their conventional long form name, three are dictatorships: North Korea (Democratic People's Republic of Korea), Laos (Lao People's Democratic Republic) and the Democratic republic of the Congo.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > 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


  Results from FactBites:
 
Using Namespaces in C++ - Cprogramming.com (544 words)
By enabling this program structure, C++ makes it easier for you to divide up a program into groups that each perform their own separate functions, in the same way that classes or structs simplify object oriented design.
One trick with namespaces is to use an unnamed namespace to avoid naming conflicts.
When your program is compiled, the "anonymous" namespace you have created will be accessible within the file you created it in.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.