• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

java import and c #include

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello evrybody,
i wanted to know the exact difference between the java import and c #include statement.basically i wanted to know their exact working.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can only talk about Java import. It tells the compiler where to find a class file. It's a shorthand so you don't have to type the full package path to every class name you reference.

For example these two snippets do the same thing:

Either way, the compiler puts the full package path to the List and ArrayList in the compiled class file for your code. That's what's needed at runtime.

You can also use the "dot star" import approach. This is considered sloppy and doesn't always work. For example if you import package.path.* on two packages and they happen to have the same classname in both, the compiler can't tell which one you want. The complete import is good documentation, too. I often use the dot star for quick coding and then ask Eclipse to fix it for me.

I used a vendor package that used the full classname on every reference. It was unreadable. The package and class names were so long that the simplest assignment statement was too long to read without scrolling. Use import. Don't use star.
[ January 21, 2007: Message edited by: Stan James ]
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import is closer to c++ namespaces then the #include directive. #include tells the preprocessor to include a copy of that specific header file. It is required if you need to use libraries. import is not required.

#include has a transitive relationship, something import does not have. If you include B in A and B includes C, C is automatically included in A. This is necessary in C and C++ since implementation and interface are separated.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#include actually is a pre-processor statement. It simply pastes the full content of the file at that position. Only after that the compiler starts to parse the resulting text.

The import statement is just syntactic sugar so that you don't always have to type the fully qualified class name.

So the two statements don't really have much in common at all.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic