Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'import' in Java vs. '#include' in C/C++

Tags:

Does 'import' in Java behave in the same way as '#include' in C/C++? Specifically, will it include the entire library that it is importing or will it just include the classes and methods that are called in the subsequent code?

like image 857
Eamon Moloney Avatar asked Nov 22 '12 17:11

Eamon Moloney


People also ask

What does * mean in Java imports?

import is a Java keyword. It declares a Java class to use in the code below the import statement. Once a Java class is declared, then the class name can be used in the code without specifying the package the class belongs to. Use the '*' character to declare all the classes belonging to the package.

What is import for in Java?

The import keyword is used to import a package, class or interface.

What is difference between #include and import?

#import and #include are preprocessor directives for bringing in the contents of a header to a file. #include is replaced by the contents of the header directly, while #import is only replaced by the contents of the header the first time that header is imported.

Is there import as in Java?

There is no import aliasing mechanism in Java. You cannot import two classes with the same name and use both of them unqualified. Import one class and use the fully qualified name for the other one, i.e.


1 Answers

#include does none of both, neither "importing" libraries, nor classes or modules.

The #include directive just tells the pre-processor to include the contents of another text file (source). That's all.

The result of pre-processing file A #includeing file B is passed to the compiler as if they were one file, with file B pasted into file A at the position where the #include directive was placed.

To expliclity state this: This all happens prior to any compilation, code generation.

As a side effect the C/C++ pre-processor could be used independently from the compiler to process any kind of text file input.

One could argue that pre-processor statements like #include "are not really part of the C/C++ languages", as they are not essentially needed to write any programs in C/C++, as they are never passed to the compiler.

The expression import is not used in the context of (standard) C/C++ programming, as there is nothing to be imported.

C/C++ modules are put together either on source level prior to compilation or by the linker after compilation.

like image 143
alk Avatar answered Oct 12 '22 14:10

alk