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?
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.
The import keyword is used to import a package, class or interface.
#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.
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.
#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 #include
ing 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With