20

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?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Eamon Moloney
  • 1,853
  • 4
  • 19
  • 22
  • Actual source is linked only at link time. Preprocessor just substitude header files containing prototypes. Please see my answer too http://stackoverflow.com/a/19581919/986760 – fkl Oct 25 '13 at 05:45

9 Answers9

25

#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.

alk
  • 69,737
  • 10
  • 105
  • 255
  • That's a good explanation overall +1. A couple points though. Doesn't the preprocessor copy the header files containing function prototypes only and the actual source files are included at link time? The above make it sound like actual sources are pasted by preprocessor, which i believe is not the case. The other point i would ask is, what exactly happens in case of import in java that is different? does the compiler perform this addition of packages itself? – fkl Oct 25 '13 at 05:37
  • @fayyazkl: The linker deals with the results of compilation. Sources (neither `*.c` nor `*.h`) are involved anymore at this stage of the build. – alk Oct 25 '13 at 11:45
  • @fayyazkl: Regarding on how Java's import works, you might like to read here: http://stackoverflow.com/questions/12620369/how-java-import-works – alk Oct 25 '13 at 11:51
  • Yes, that's correct so existing headers included are precompiled. Hence object files are only linked at link time. If they are not compiled, that should be done as part of compilation phase. Again no reference to source in the preprocessor phase which was my point. – fkl Oct 25 '13 at 12:11
  • @fayyazkl: "*...no reference to source in the preprocessor phase ...*" Hu? Sources (`*.c`and `*.h`) are read during pre-processing phase, to create an intemediate version of every `*.c`file (having the `*.h` s merged into the `*.c`s which `#include`d them). Those intemediate **sources** are then fed to the compiler to create object files, which then in turn are passed to the linker along with the required libraries (if any) to create the final executable or library. – alk Oct 25 '13 at 12:19
  • .c of an include file read in preprocessor phase? I don't think so. It is only .h. My intention was .c = source, .h = header file, not source – fkl Oct 25 '13 at 12:23
  • @fayyazkl: In this context my defintion of "sources" is everything containing C code, that is `*.c` **and** `*.h`. These files are the "source" from which the the program/library is to be build. – alk Oct 25 '13 at 12:26
  • @fayyazkl: For your kind reference as further readings: In particular: http://gcc.gnu.org/onlinedocs/cpp/Initial-processing.html#Initial-processing in general: http://gcc.gnu.org/onlinedocs/cpp/index.html#Top – alk Oct 25 '13 at 12:31
11

#include<stdio.h> means copy the contents of stdio.h in this file, while import java.util.ArrayList means if you cann't find a class in this file look into above mentioned import location. Java imports do not increase size of your file it just saves few keystrokes.

Ajay Sharma
  • 846
  • 10
  • 21
7

In Java, when you use import, you import either :

  • a single class : import foo.bar.Baz
  • a full package : import foo.bar.*
  • a single static member of a class : import static foo.bar.Baz.GRUT
  • every static member of a class : import static foo.bar.Baz.*
Jerome
  • 2,104
  • 1
  • 17
  • 31
2

C/C++ #include directive happens at pre-processing phase and in plain English it means "at this place paste the whole header file(or any text file if you like) that is given as parameter". The new C++ standard will have modules (finally), and may or may not be similar to Java's import (depends how it is going to be implemented). More about this C++ proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3347.pdf

Java 8 will also have improved support for modular programming. See: http://openjdk.java.net/projects/jigsaw/

Gab是好人
  • 1,976
  • 1
  • 25
  • 39
DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • 1
    `#include`ing does not happend at compile time, but **prior** to compilation. – alk Nov 22 '12 at 18:14
  • I did not want into details about how C/C++ compiler processes the source, that would totally be off-topic. Do you agree? Still the comment is fair, so +1. – DejanLekic Nov 22 '12 at 18:18
  • In the context of finding the difference(s) between Java's `import` statement and C's/C++'s `#include` directive (as questioned by the OP) I do not think it is off-topic to point out that the C/C++ pre-processor isn't part of any compilation or code generation, but an essential fact to mention. – alk Nov 22 '12 at 18:59
  • Anyway, +1 for mentioning possible upcoming changes to the classical C/C++ approach, at least for C++. – alk Nov 22 '12 at 19:03
  • #include only puts in prototype in preprocessor, not definitions. That is done on link time. See my answer, you can even test it with only using a function prototype without definition. http://stackoverflow.com/a/19581919/986760 – fkl Oct 25 '13 at 05:46
  • 1
    @fayyazkl: An `#include` directive does simply include a text file, no matter what this file actullay contains. The latter is fully left to the programmer and could very well include definitions of any kind. – alk May 09 '15 at 13:47
1

I think one aspect that other answers might have not cleared regarding c is,

include in c only copies the header file in the preprocessor phase which contains the function prototype, nothing more. The actual function definition is still located at link time, after basic code compilation.

A simple verification can be done by including a header file, but not the source file and calling the function from your code. There will be no compilation error and only at link time it would complain not being able to locate the definition.

fkl
  • 5,412
  • 4
  • 28
  • 68
0

Depends on what you actually import. The smallest importable entity is class, the largest is the package.
So if you need a single class and import complete package it will bring in the complete package.

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
0

import (in Java) is similar to using (in C++) - As you understand they are not exactly same, but very similar.

import is to indicate the compiler where it can find the class (or sub package) used in the current class.

include - Sven explained it better here - https://stackoverflow.com/a/3739563/135553

Community
  • 1
  • 1
Narendra N
  • 1,270
  • 1
  • 9
  • 14
-1

In general terms :

In c language, when compiler encounters #include statement , all the specified header files will be loaded at the time of include statement only irrespective of whether we are using those header files or not. Hence it is called static binding .

But in case of java language, when compiler encounters import statement no .class file will be loaded at the time of import statement. In the next lines of code whenever we are using a class at that time only the corresponding .class file will be loaded. This type of loading is called dynamic loading or load on demand or load on fly.

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34
  • "*In c language, when compiler encounters #include statement*" a pure text replacement is taking place. This is completely unrelated to any "binding", which if done is done after pre-processing, after compiling, by the linker. – alk May 23 '19 at 04:53
-2

include usually refers to C,C++ lang,where they are of platform dependent languages and need to be compiled,linked and loaded on directly to the machine instruction set of architecture, but where as import refers to java lang,it is of platform independent ,where it can generate byte code and then it can interpreted to binary code format.