(Based on my somewhat limited understanding of Java) Java uses heirarchies of packages to organise libraries of classes, which is a far cry from the header system that C++ inherited from 'C', although the namespace system provides some of the benefits of java's package system. With that in mind, it wouldn't make sense for the two keywords to be direct equivalents, Although that's probably the closest comparison you'll get.
the C++ 'using' keyword allows you to select as few or as many names from a class or namespace as you like, to be visible without needing the :: operator (the scope operator) when referring to the name.
Most of the time, the easiest thing to do when starting out learning C++ is to type one using directive at the top of every program
Code:
using namespace std;
in tiny test programs, name clashes are easily avoided, so you can get away with that. But in larger programs, you'll quickly discover that alot of common, useful names are already taken up by the standard library. Still, you can be a little lazy with some of the names
Code:
#include <iostream>
#include <string>
using std::cout;
using std::string;
int main()
{
//Just suppose we come up with a very poor name for a string variable
string cin = "Hello World";
cout << cin;
} Of course, noone in their right mind would go about naming a variable 'cin', because its just about one of the most common names used by the C++ standard library (Unless they wanted to totally throw anyone who was reading their code). But as you can see, the program compiles, because std::cin hasn't been brought into the global namespace. If the above program had contained
using namespace std; there'd have been a compiler error.
Depending where you place the using keyword, you can localise its effect. for example, If you don't want to bring the std namespace anywhere into your program, except for main, you can do
Code:
#include <iostream>
#include <string>
int main()
{
using namespace std;
string my_str = "Hello World";
cout << my_str;
} Which limits the effect of
using namespace std; to the main() function.
Best practice though, once you get to that stage, is to avoid the temptations of laziness, and use fully-qualified names instead (That is, names where you start out with std:: )