I am moving from java programming to C++ and I had a confusion regarding #include
and using namespace ...
Would #include
be the equivalent of java imports e.g. java.lang.whateverClass;
or is that what namespace would be like? Or am I completely wrong all together? I appreciate the help and if there is a better place to send me I would appreciate that also.

- 11,202
- 14
- 64
- 112
-
See: http://stackoverflow.com/a/2108209/609074 – PhD Apr 22 '14 at 23:45
-
`import` is for a class (in a jar or class file) in Java. Once you import it, it would be under global "namespace" (quoted because there is no such thing in Java). `include` is for a file, which may contain one or more classes in different namespaces. `using namespace XX` is to share all classes/functions/etc. under the namespace to global. – SwiftMango Apr 23 '14 at 02:47
2 Answers
namespace
is more like package
, and #include
is a bit like import
, except that in Java when you import a class it only makes the simple name of the class available. So, you could always just write the fully qualified name of the class everywhere instead of importing it. But with C and C++, when you #include
a header file, the compiler will process everything in that header file while it is compiling. So, while #include
is similar to import
in its purpose, the details of how it works are quite different.

- 15,432
- 2
- 42
- 54
No, #include
is nothing like import
in Java. In Java import
is only helping you in the sense that you don't need to type the full class name (including package). In C++ it includes a file. Literally.
namespaces however are used in the same way as packages in Java. Packages in Java are namespaces.

- 90,524
- 13
- 150
- 263
-
for more clarification, asker can refer to [this question](http://stackoverflow.com/questions/2108172/c-namespaces-comparison-to-java-packages) – maxx777 Apr 22 '14 at 23:46
-
@maxx777 packages in Java always *seem* to have a very deep nested structure. Actually, they don't. Each and every package is simply its own namespace, no matter how many dots, parents or whatever. As indicated, this will change a bit when Java gets more modular. – Maarten Bodewes Apr 22 '14 at 23:49
-
yeah. this was the doubt that had raised in my mind around two years back. It got clarified after going through the above link. But now out of my curiosity, i would like to know how they are gonna change? somewhat nested structure? ps: i was not the asker of original question :p – maxx777 Apr 22 '14 at 23:52
-
@maxx777 hmm, I don't see too much in the current [JigSaw](http://openjdk.java.net/projects/jigsaw/) requirements, so I've shortened the last sentence a bit; seems like packages will remain just namespaces for now. – Maarten Bodewes Apr 23 '14 at 00:30