• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference b/w import & #include in C++

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Can any body tell me the difference the difference b/w import & #include in C++ ?
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohit,

In short, there's a lot of difference.

The Java import statement is effectively just a declaration - it doesn't include a file. It's function is to let the compiler know where (i.e. in which package) to locate the API for the imported class. It's also a convenient shorthand so that you don't have to include the full package specification each time you refer to a class. I think it may also be used, indirectly, by the JVM to determine which classes to load at start-up. Perhaps someone else can clarify that.

The C/C++ #include directive actually includes a file, usually a header file, inline as the C source file is compiled.

Java has little or no need for most of the features usually provided in a C/C++ header file. Consider these:
  • Java is platform independent and therefore has little use for preprocessor directives such as #define, #ifdef, etc.
  • Lists of global constants can be encapsulated as static final declarations in an interface
  • Java doesn't have macros, but the performance gains offered by these in C/C++ are mitigated by the use of inlining by Java compilers
  • You don't need to define function prototypes in Java as the compiler copes without forward definitions (two-pass compilation??)


  • I hope this adequately answers your question.

    Jules
     
    Mohit Agarwal
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank You Julian for the reply, its worth providing me an insight and helping me to think and explore more about it.
     
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The way I look at it is this way:

    #include <myheader.h>
    Embeds the code in myheader.h into your program sourcecode (.cpp).

    import java.util.Color;
    Shortcuts the code from Color into your program sourcecode (.java).
    -------------------------------

    If you don't do this:
    import java.util.Color;

    Then everytime you want a color you have to do this:
    Color c = java.util.Color.red;
    -------------------------------

    If you do do this:
    import java.util.Color;

    Then everytime you want a color you can do this:
    Color c = Color.red;
    [ August 07, 2004: Message edited by: Jack Kay ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic