21

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?

1
  • Actual source is linked only at link time. Preprocessor just substitude header files containing prototypes. Please see my answer too stackoverflow.com/a/19581919/986760
    – fkl
    Oct 25, 2013 at 5:45

9 Answers 9

26

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

8
  • 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, 2013 at 5: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, 2013 at 11:45
  • @fayyazkl: Regarding on how Java's import works, you might like to read here: stackoverflow.com/questions/12620369/how-java-import-works
    – alk
    Oct 25, 2013 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, 2013 at 12:11
  • @fayyazkl: "...no reference to source in the preprocessor phase ..." Hu? Sources (*.cand *.h) are read during pre-processing phase, to create an intemediate version of every *.cfile (having the *.h s merged into the *.cs which #included 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, 2013 at 12:19
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.

1
  • 1
    Thanks. This was what I was looking for.
    – h8pathak
    Sep 3, 2016 at 13:55
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.*
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.

1

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/

6
  • 1
    #includeing does not happend at compile time, but prior to compilation.
    – alk
    Nov 22, 2012 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, 2012 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, 2012 at 18:59
  • Anyway, +1 for mentioning possible upcoming changes to the classical C/C++ approach, at least for C++.
    – alk
    Nov 22, 2012 at 19:03
  • 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 9, 2015 at 13:47
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.

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

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

1
  • "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, 2019 at 4: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.

1
  • The question was not "C/C+" vs. "Java".
    – alk
    May 9, 2015 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.